1
0
Fork 0
mirror of https://github.com/Zedfrigg/ironbar.git synced 2025-07-03 19:51:03 +02:00

chore: initial commit

This commit is contained in:
Jake Stanger 2022-08-14 14:30:13 +01:00
commit e37d8f2b14
No known key found for this signature in database
GPG key ID: C51FC8F9CB0BEA61
36 changed files with 4948 additions and 0 deletions

87
src/popup.rs Normal file
View file

@ -0,0 +1,87 @@
use gtk::prelude::*;
use gtk::{Application, ApplicationWindow, Orientation};
#[derive(Debug, Clone)]
pub struct Popup {
pub window: ApplicationWindow,
pub container: gtk::Box,
}
pub enum PopupAlignment {
Left,
Center,
Right,
}
impl Popup {
pub fn new(name: &str, app: &Application, orientation: Orientation) -> Self {
let win = ApplicationWindow::builder().application(app).build();
gtk_layer_shell::init_for_window(&win);
gtk_layer_shell::set_layer(&win, gtk_layer_shell::Layer::Overlay);
gtk_layer_shell::set_margin(&win, gtk_layer_shell::Edge::Top, 0);
gtk_layer_shell::set_margin(&win, gtk_layer_shell::Edge::Bottom, 5);
gtk_layer_shell::set_margin(&win, gtk_layer_shell::Edge::Left, 0);
gtk_layer_shell::set_margin(&win, gtk_layer_shell::Edge::Right, 0);
gtk_layer_shell::set_anchor(&win, gtk_layer_shell::Edge::Top, false);
gtk_layer_shell::set_anchor(&win, gtk_layer_shell::Edge::Bottom, true);
gtk_layer_shell::set_anchor(&win, gtk_layer_shell::Edge::Left, true);
gtk_layer_shell::set_anchor(&win, gtk_layer_shell::Edge::Right, false);
let content = gtk::Box::builder()
.orientation(orientation)
.spacing(0)
.hexpand(false)
.name(name)
.build();
content.style_context().add_class("popup");
win.add(&content);
win.connect_leave_notify_event(|win, ev| {
let (w, _h) = win.size();
let (x, y) = ev.position();
const THRESHOLD: f64 = 3.0;
// some child widgets trigger this event
// so check we're actually outside the window
if x < THRESHOLD || y < THRESHOLD || x > f64::from(w) - THRESHOLD {
win.hide();
}
Inhibit(false)
});
Self {
window: win,
container: content,
}
}
/// Sets the popover's X position relative to the left border of the screen
pub fn set_pos(&self, pos: f64, alignment: PopupAlignment) {
let width = self.window.allocated_width();
let offset = match alignment {
PopupAlignment::Left => pos,
PopupAlignment::Center => (pos - (f64::from(width) / 2.0)).round(),
PopupAlignment::Right => pos - f64::from(width),
};
gtk_layer_shell::set_margin(&self.window, gtk_layer_shell::Edge::Left, offset as i32);
}
/// Shows the popover
pub fn show(&self) {
self.window.show_all();
}
/// Hides the popover
pub fn hide(&self) {
self.window.hide();
}
}