Skip to content

Commit

Permalink
fix(popup): often opening in wrong place
Browse files Browse the repository at this point in the history
Fixes #16.
  • Loading branch information
JakeStanger committed Oct 14, 2022
1 parent 9e31107 commit 5523e9a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ fn add_modules(

let popup = popup.read().expect("Failed to get read lock on popup");
popup.hide();
popup.show(x, w);
popup.show_content($id);
popup.show(x, w);
}
ModuleUpdateEvent::ClosePopup => {
debug!("Closing popup for {} [#{}]", $name, $id);
Expand Down
32 changes: 27 additions & 5 deletions src/modules/launcher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,15 @@ impl Module<gtk::Box> for LauncherModule {
controller_tx: Sender<Self::ReceiveMessage>,
rx: glib::Receiver<Self::SendMessage>,
) -> Option<gtk::Box> {
let container = gtk::Box::new(Orientation::Vertical, 0);
const MAX_WIDTH: i32 = 250;

let container = gtk::Box::builder()
.orientation(Orientation::Vertical)
.build();

let placeholder = Button::with_label("PLACEHOLDER");
placeholder.set_width_request(MAX_WIDTH);
container.add(&placeholder);

let mut buttons = Collection::<String, Collection<usize, Button>>::new();

Expand All @@ -432,9 +440,8 @@ impl Module<gtk::Box> for LauncherModule {
.into_iter()
.map(|win| {
let button = Button::builder()
.label(&win.name)
.label(&clamp(&win.name))
.height_request(40)
.width_request(100)
.build();

{
Expand All @@ -458,9 +465,8 @@ impl Module<gtk::Box> for LauncherModule {
LauncherUpdate::AddWindow(app_id, win) => {
if let Some(buttons) = buttons.get_mut(&app_id) {
let button = Button::builder()
.label(&win.name)
.height_request(40)
.width_request(100)
.label(&clamp(&win.name))
.build();

{
Expand Down Expand Up @@ -503,6 +509,7 @@ impl Module<gtk::Box> for LauncherModule {
}

container.show_all();
container.set_width_request(MAX_WIDTH);
}
}
_ => {}
Expand All @@ -515,3 +522,18 @@ impl Module<gtk::Box> for LauncherModule {
Some(container)
}
}

/// Clamps a string at 24 characters.
///
/// This is a hacky number derived from
/// "what fits inside the 250px popup"
/// and probably won't hold up with wide fonts.
fn clamp(str: &str) -> String {
const MAX_CHARS: usize = 24;

if str.len() > MAX_CHARS {
str.chars().take(MAX_CHARS - 3).collect::<String>() + "..."
} else {
str.to_string()
}
}
2 changes: 1 addition & 1 deletion src/popup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl Popup {
/// Sets the popover's X position relative to the left border of the screen
fn set_pos(&self, button_x: i32, button_width: i32) {
let screen_width = self.monitor.workarea().width();
let popup_width = self.window.allocated_width();
let (popup_width, _popup_height) = self.window.size();

let widget_center = f64::from(button_x) + f64::from(button_width) / 2.0;

Expand Down

0 comments on commit 5523e9a

Please sign in to comment.