Skip to content

Commit

Permalink
Update the title that affects the inner size
Browse files Browse the repository at this point in the history
  • Loading branch information
Zamoca42 committed Oct 4, 2024
1 parent 971de4f commit a96dd7f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
28 changes: 26 additions & 2 deletions src/platform_impl/linux/wayland/header.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use gtk::{
glib::{self},
pango,
prelude::*,
ApplicationWindow, EventBox, HeaderBar,
Align, ApplicationWindow, EventBox, HeaderBar, Label,
};

pub struct WlHeader;
Expand All @@ -11,9 +12,13 @@ impl WlHeader {
let header = HeaderBar::builder()
.show_close_button(true)
.decoration_layout("menu:minimize,maximize,close")
.title(title)
.build();

let title_label = Label::new(Some(title));
title_label.set_ellipsize(gtk::pango::EllipsizeMode::End);
title_label.set_single_line_mode(true);
title_label.set_halign(Align::Center);

let event_box = EventBox::new();
event_box.set_above_child(true);
event_box.set_visible(true);
Expand All @@ -27,11 +32,30 @@ impl WlHeader {
header_clone.set_size_request(min_width, allocated_height);
});

header.set_custom_title(Some(&title_label));
event_box.add(&header);
window.set_titlebar(Some(&event_box));

//Set title font width
let context = title_label.pango_context();
let font_description = context.font_description().unwrap();
let font_size = (font_description.size() / pango::SCALE) as f64;
let char_width = font_size * 2.0;

Self::connect_configure_event(window, &title_label, char_width);
Self::connect_resize_window(&header, window);
}

fn connect_configure_event(window: &ApplicationWindow, title_label: &Label, char_width: f64) {
let title_label_clone = title_label.clone();
window.connect_configure_event(move |_, event| {
let (width, _) = event.size();
let max_chars = (width as f64 / char_width).floor() as i32;
title_label_clone.set_max_width_chars(if width < 220 { 0 } else { max_chars });
false
});
}

fn connect_resize_window(header: &HeaderBar, window: &ApplicationWindow) {
let header_weak = header.downgrade();
window.connect_resizable_notify(move |window| {
Expand Down
1 change: 0 additions & 1 deletion src/platform_impl/linux/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ impl Window {

let window_clone = window.clone();
glib::idle_add_local_once(move || {
window_clone.set_resizable(true);
window_clone.set_default_size(min_width, min_height);
window_clone.resize(width, height);
});
Expand Down

0 comments on commit a96dd7f

Please sign in to comment.