Skip to content

Commit

Permalink
fix(popup): re-position on resize due to content change
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeStanger committed Feb 18, 2024
1 parent a55ba8c commit a10466e
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 67 deletions.
11 changes: 5 additions & 6 deletions src/bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ use gtk::gdk::Monitor;
use gtk::prelude::*;
use gtk::{Application, ApplicationWindow, IconTheme, Orientation, Window, WindowType};
use gtk_layer_shell::LayerShell;
use std::cell::RefCell;
use std::rc::Rc;
use std::time::Duration;
use tracing::{debug, info};

#[derive(Debug, Clone)]
enum Inner {
New { config: Option<Config> },
Loaded { popup: Rc<RefCell<Popup>> },
Loaded { popup: Rc<Popup> },
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -269,7 +268,7 @@ impl Bar {

// popup ignores module location so can bodge this for now
let popup = Popup::new(&info!(ModuleLocation::Left), config.popup_gap);
let popup = Rc::new(RefCell::new(popup));
let popup = Rc::new(popup);

if let Some(modules) = config.start {
let info = info!(ModuleLocation::Left);
Expand Down Expand Up @@ -315,7 +314,7 @@ impl Bar {
&self.monitor_name
}

pub fn popup(&self) -> Rc<RefCell<Popup>> {
pub fn popup(&self) -> Rc<Popup> {
match &self.inner {
Inner::New { .. } => {
panic!("Attempted to get popup of uninitialized bar. This is a serious bug!")
Expand All @@ -339,7 +338,7 @@ fn create_container(name: &str, orientation: Orientation) -> gtk::Box {

#[derive(Debug)]
struct BarLoadResult {
popup: Rc<RefCell<Popup>>,
popup: Rc<Popup>,
}

/// Adds modules into a provided GTK box,
Expand All @@ -349,7 +348,7 @@ fn add_modules(
modules: Vec<ModuleConfig>,
info: &ModuleInfo,
ironbar: &Rc<Ironbar>,
popup: &Rc<RefCell<Popup>>,
popup: &Rc<Popup>,
) -> Result<()> {
let orientation = info.bar_position.orientation();

Expand Down
15 changes: 7 additions & 8 deletions src/ipc/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,21 +167,20 @@ impl Ipc {
match bar {
Some(bar) => {
let popup = bar.popup();
let current_widget = popup.borrow().current_widget();
let current_widget = popup.current_widget();

popup.borrow_mut().hide();
popup.hide();

let data = popup
.borrow()
.cache
.borrow()
.iter()
.find(|(_, value)| value.name == name)
.map(|(id, value)| (*id, value.content.buttons.first().cloned()));

match data {
Some((id, Some(button))) if current_widget != Some(id) => {
let button_id = button.popup_id();
let mut popup = popup.borrow_mut();

if popup.is_visible() {
popup.hide();
Expand All @@ -207,19 +206,19 @@ impl Ipc {
let popup = bar.popup();

// only one popup per bar, so hide if open for another widget
popup.borrow_mut().hide();
popup.hide();

let data = popup
.borrow()
.cache
.borrow()
.iter()
.find(|(_, value)| value.name == name)
.map(|(id, value)| (*id, value.content.buttons.first().cloned()));

match data {
Some((id, Some(button))) => {
let button_id = button.popup_id();
popup.borrow_mut().show(id, button_id);
popup.show(id, button_id);

Response::Ok
}
Expand All @@ -236,7 +235,7 @@ impl Ipc {
match bar {
Some(bar) => {
let popup = bar.popup();
popup.borrow_mut().hide();
popup.hide();

Response::Ok
}
Expand Down
23 changes: 3 additions & 20 deletions src/modules/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::cell::RefCell;
use std::fmt::Debug;
use std::rc::Rc;
use std::sync::Arc;
Expand Down Expand Up @@ -215,7 +214,7 @@ pub fn create_module<TModule, TWidget, TSend, TRec>(
ironbar: Rc<Ironbar>,
name: Option<String>,
info: &ModuleInfo,
popup: &Rc<RefCell<Popup>>,
popup: &Rc<Popup>,
) -> Result<ModuleParts<TWidget>>
where
TModule: Module<TWidget, SendMessage = TSend, ReceiveMessage = TRec>,
Expand Down Expand Up @@ -251,24 +250,14 @@ where
.style_context()
.add_class(&format!("popup-{module_name}"));

register_popup_content(popup, id, instance_name, popup_content);
popup.register_content(id, instance_name, popup_content);
}

setup_receiver(tx, ui_rx, popup.clone(), module_name, id);

Ok(module_parts)
}

/// Registers the popup content with the popup.
fn register_popup_content(
popup: &Rc<RefCell<Popup>>,
id: usize,
name: String,
popup_content: ModulePopupParts,
) {
popup.borrow_mut().register_content(id, name, popup_content);
}

/// Sets up the bridge channel receiver
/// to pick up events from the controller, widget or popup.
///
Expand All @@ -277,7 +266,7 @@ fn register_popup_content(
fn setup_receiver<TSend>(
tx: broadcast::Sender<TSend>,
rx: mpsc::Receiver<ModuleUpdateEvent<TSend>>,
popup: Rc<RefCell<Popup>>,
popup: Rc<Popup>,
name: &'static str,
id: usize,
) where
Expand All @@ -294,7 +283,6 @@ fn setup_receiver<TSend>(
}
ModuleUpdateEvent::TogglePopup(button_id) => {
debug!("Toggling popup for {} [#{}]", name, id);
let mut popup = popup.borrow_mut();
if popup.is_visible() {
popup.hide();
} else {
Expand All @@ -309,8 +297,6 @@ fn setup_receiver<TSend>(
}
ModuleUpdateEvent::OpenPopup(button_id) => {
debug!("Opening popup for {} [#{}]", name, id);

let mut popup = popup.borrow_mut();
popup.hide();
popup.show(id, button_id);

Expand All @@ -324,7 +310,6 @@ fn setup_receiver<TSend>(
ModuleUpdateEvent::OpenPopupAt(geometry) => {
debug!("Opening popup for {} [#{}]", name, id);

let mut popup = popup.borrow_mut();
popup.hide();
popup.show_at(id, geometry);

Expand All @@ -336,8 +321,6 @@ fn setup_receiver<TSend>(
}
ModuleUpdateEvent::ClosePopup => {
debug!("Closing popup for {} [#{}]", name, id);

let mut popup = popup.borrow_mut();
popup.hide();
}
}
Expand Down
Loading

0 comments on commit a10466e

Please sign in to comment.