start porting to gtk4

This commit is contained in:
Jake Stanger
2023-04-22 00:29:50 +01:00
parent 0e3102de8c
commit c73585324c
19 changed files with 76 additions and 78 deletions

View File

@@ -46,9 +46,9 @@ workspaces = ["futures-util"]
[dependencies] [dependencies]
# core # core
gtk = "0.17.0" gtk = { package = "gtk4", version = "0.6.6" }
gtk-layer-shell = "0.6.0" gtk-layer-shell = { package = "gtk4-layer-shell", version = "0.0.3" }
glib = "0.17.5" glib = "0.17.9"
tokio = { version = "1.21.2", features = ["macros", "rt-multi-thread", "time", "process", "sync", "io-util", "net"] } tokio = { version = "1.21.2", features = ["macros", "rt-multi-thread", "time", "process", "sync", "io-util", "net"] }
tracing = "0.1.37" tracing = "0.1.37"
tracing-subscriber = { version = "0.3.16", features = ["env-filter"] } tracing-subscriber = { version = "0.3.16", features = ["env-filter"] }

View File

@@ -7,6 +7,7 @@ use gtk::gdk::Monitor;
use gtk::prelude::*; use gtk::prelude::*;
use gtk::{Application, ApplicationWindow, IconTheme, Orientation}; use gtk::{Application, ApplicationWindow, IconTheme, Orientation};
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
use glib::signal::Inhibit;
use tracing::{debug, info}; use tracing::{debug, info};
/// Creates a new window for a bar, /// Creates a new window for a bar,
@@ -48,16 +49,16 @@ pub fn create_bar(
let center = create_container("center", orientation); let center = create_container("center", orientation);
let end = create_container("end", orientation); let end = create_container("end", orientation);
content.add(&start); content.append(&start);
content.set_center_widget(Some(&center)); content.set_center_widget(Some(&center));
content.pack_end(&end, false, false, 0); content.pack_end(&end, false, false, 0);
load_modules(&start, &center, &end, app, config, monitor, monitor_name)?; load_modules(&start, &center, &end, app, config, monitor, monitor_name)?;
win.add(&content); win.append(&content);
win.connect_destroy_event(|_, _| { win.connect_destroy_event(|_, _| {
info!("Shutting down"); info!("Shutting down");
gtk::main_quit(); // gtk::main_quit();
Inhibit(false) Inhibit(false)
}); });
@@ -197,7 +198,7 @@ fn add_modules(
let common = $module.common.take().expect("Common config did not exist"); let common = $module.common.take().expect("Common config did not exist");
let widget = create_module(*$module, $id, &info, &Arc::clone(&popup))?; let widget = create_module(*$module, $id, &info, &Arc::clone(&popup))?;
let container = wrap_widget(&widget, common, orientation); let container = wrap_widget(&widget, common, orientation);
content.add(&container); content.append(&container);
}}; }};
} }

View File

@@ -1,9 +1,10 @@
use glib::signal::Inhibit;
use crate::dynamic_string::DynamicString; use crate::dynamic_string::DynamicString;
use crate::script::{Script, ScriptInput}; use crate::script::{Script, ScriptInput};
use crate::send; use crate::send;
use gtk::gdk::ScrollDirection; use gtk::gdk::ScrollDirection;
use gtk::prelude::*; use gtk::prelude::*;
use gtk::{EventBox, Orientation, Revealer, RevealerTransitionType}; use gtk::{GestureClick, Orientation, Revealer, RevealerTransitionType, Widget};
use serde::Deserialize; use serde::Deserialize;
use tokio::spawn; use tokio::spawn;
use tracing::trace; use tracing::trace;
@@ -55,14 +56,16 @@ impl TransitionType {
impl CommonConfig { impl CommonConfig {
/// Configures the module's container according to the common config options. /// Configures the module's container according to the common config options.
pub fn install(mut self, container: &EventBox, revealer: &Revealer) { pub fn install<W: IsA<Widget>>(mut self, widget: &W, revealer: &Revealer) {
self.install_show_if(container, revealer); self.install_show_if(widget, revealer);
let left_click_script = self.on_click_left.map(Script::new_polling); let left_click_script = self.on_click_left.map(Script::new_polling);
let middle_click_script = self.on_click_middle.map(Script::new_polling); let middle_click_script = self.on_click_middle.map(Script::new_polling);
let right_click_script = self.on_click_right.map(Script::new_polling); let right_click_script = self.on_click_right.map(Script::new_polling);
container.connect_button_press_event(move |_, event| { let gesture = GestureClick::new();
gesture.connect_pressed(move |_, event| {
let script = match event.button() { let script = match event.button() {
1 => left_click_script.as_ref(), 1 => left_click_script.as_ref(),
2 => middle_click_script.as_ref(), 2 => middle_click_script.as_ref(),
@@ -74,14 +77,12 @@ impl CommonConfig {
trace!("Running on-click script: {}", event.button()); trace!("Running on-click script: {}", event.button());
script.run_as_oneshot(None); script.run_as_oneshot(None);
} }
Inhibit(false)
}); });
let scroll_up_script = self.on_scroll_up.map(Script::new_polling); let scroll_up_script = self.on_scroll_up.map(Script::new_polling);
let scroll_down_script = self.on_scroll_down.map(Script::new_polling); let scroll_down_script = self.on_scroll_down.map(Script::new_polling);
container.connect_scroll_event(move |_, event| { widget.connect_scroll_event(move |_, event| {
let script = match event.direction() { let script = match event.direction() {
ScrollDirection::Up => scroll_up_script.as_ref(), ScrollDirection::Up => scroll_up_script.as_ref(),
ScrollDirection::Down => scroll_down_script.as_ref(), ScrollDirection::Down => scroll_down_script.as_ref(),
@@ -99,7 +100,7 @@ impl CommonConfig {
macro_rules! install_oneshot { macro_rules! install_oneshot {
($option:expr, $method:ident) => { ($option:expr, $method:ident) => {
$option.map(Script::new_polling).map(|script| { $option.map(Script::new_polling).map(|script| {
container.$method(move |_, _| { widget.$method(move |_, _| {
script.run_as_oneshot(None); script.run_as_oneshot(None);
Inhibit(false) Inhibit(false)
}); });
@@ -111,7 +112,7 @@ impl CommonConfig {
install_oneshot!(self.on_mouse_exit, connect_leave_notify_event); install_oneshot!(self.on_mouse_exit, connect_leave_notify_event);
if let Some(tooltip) = self.tooltip { if let Some(tooltip) = self.tooltip {
let container = container.clone(); let container = widget.clone();
DynamicString::new(&tooltip, move |string| { DynamicString::new(&tooltip, move |string| {
container.set_tooltip_text(Some(&string)); container.set_tooltip_text(Some(&string));
Continue(true) Continue(true)
@@ -119,14 +120,14 @@ impl CommonConfig {
} }
} }
fn install_show_if(&mut self, container: &EventBox, revealer: &Revealer) { fn install_show_if<W: IsA<Widget>>(&mut self, widget: &W, revealer: &Revealer) {
self.show_if.take().map_or_else( self.show_if.take().map_or_else(
|| { || {
container.show_all(); widget.set_visible(true)
}, },
|show_if| { |show_if| {
let script = Script::new_polling(show_if); let script = Script::new_polling(show_if);
let container = container.clone(); let widget = widget.clone();
let (tx, rx) = glib::MainContext::channel(glib::PRIORITY_DEFAULT); let (tx, rx) = glib::MainContext::channel(glib::PRIORITY_DEFAULT);
spawn(async move { spawn(async move {

View File

@@ -38,7 +38,7 @@ pub fn new_icon_label(input: &str, icon_theme: &IconTheme, size: i32) -> gtk::Bo
let image = Image::new(); let image = Image::new();
image.set_widget_name("image"); image.set_widget_name("image");
container.add(&image); container.append(&image);
if let Err(err) = ImageProvider::parse(input, icon_theme, size) if let Err(err) = ImageProvider::parse(input, icon_theme, size)
.and_then(|provider| provider.load_into_image(image)) .and_then(|provider| provider.load_into_image(image))
@@ -49,7 +49,7 @@ pub fn new_icon_label(input: &str, icon_theme: &IconTheme, size: i32) -> gtk::Bo
let label = Label::new(Some(input)); let label = Label::new(Some(input));
label.set_widget_name("label"); label.set_widget_name("label");
container.add(&label); container.append(&label);
} }
container container

View File

@@ -21,7 +21,7 @@ use crate::style::load_css;
use color_eyre::eyre::Result; use color_eyre::eyre::Result;
use color_eyre::Report; use color_eyre::Report;
use dirs::config_dir; use dirs::config_dir;
use gtk::gdk::Display; use gtk::gdk::{Display, Monitor};
use gtk::prelude::*; use gtk::prelude::*;
use gtk::Application; use gtk::Application;
use std::env; use std::env;
@@ -120,7 +120,10 @@ fn create_bars(
debug!("Received {} outputs from Wayland", outputs.len()); debug!("Received {} outputs from Wayland", outputs.len());
debug!("Outputs: {:?}", outputs); debug!("Outputs: {:?}", outputs);
let num_monitors = display.n_monitors(); for monitor in display.monitors().iter::<Monitor>() {
let monitor = monitor.unwrap();
}
for i in 0..num_monitors { for i in 0..num_monitors {
let monitor = display let monitor = display

View File

@@ -8,10 +8,11 @@ use crate::try_send;
use gtk::gdk_pixbuf::Pixbuf; use gtk::gdk_pixbuf::Pixbuf;
use gtk::gio::{Cancellable, MemoryInputStream}; use gtk::gio::{Cancellable, MemoryInputStream};
use gtk::prelude::*; use gtk::prelude::*;
use gtk::{Button, EventBox, Image, Label, Orientation, RadioButton, Widget}; use gtk::{Button, Image, Label, Orientation, RadioButton, Widget};
use serde::Deserialize; use serde::Deserialize;
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::Arc; use std::sync::Arc;
use glib::signal::Inhibit;
use tokio::spawn; use tokio::spawn;
use tokio::sync::mpsc::{Receiver, Sender}; use tokio::sync::mpsc::{Receiver, Sender};
use tracing::{debug, error}; use tracing::{debug, error};
@@ -161,10 +162,10 @@ impl Module<Button> for ClipboardModule {
.build(); .build();
let entries = gtk::Box::new(Orientation::Vertical, 5); let entries = gtk::Box::new(Orientation::Vertical, 5);
container.add(&entries); container.append(&entries);
let hidden_option = RadioButton::new(); let hidden_option = RadioButton::new();
entries.add(&hidden_option); entries.append(&hidden_option);
let mut items = HashMap::new(); let mut items = HashMap::new();
@@ -183,7 +184,7 @@ impl Module<Button> for ClipboardModule {
let button = RadioButton::from_widget(&hidden_option); let button = RadioButton::from_widget(&hidden_option);
let label = Label::new(Some(value)); let label = Label::new(Some(value));
button.add(&label); button.append(&label);
if let Some(truncate) = self.truncate { if let Some(truncate) = self.truncate {
truncate.truncate_label(&label); truncate.truncate_label(&label);
@@ -218,7 +219,7 @@ impl Module<Button> for ClipboardModule {
button.set_active(true); // if just added, should be on clipboard button.set_active(true); // if just added, should be on clipboard
let button_wrapper = EventBox::new(); let button_wrapper = EventBox::new();
button_wrapper.add(&button); button_wrapper.append(&button);
button_wrapper.set_widget_name(&format!("copy-{id}")); button_wrapper.set_widget_name(&format!("copy-{id}"));
button_wrapper.set_above_child(true); button_wrapper.set_above_child(true);
@@ -261,12 +262,11 @@ impl Module<Button> for ClipboardModule {
}); });
} }
row.add(&button_wrapper); row.append(&button_wrapper);
row.pack_end(&remove_button, false, false, 0); row.pack_end(&remove_button, false, false, 0);
entries.add(&row); entries.append(&row);
entries.reorder_child(&row, 0); entries.reorder_child(&row, 0);
row.show_all();
items.insert(id, (row, button)); items.insert(id, (row, button));
} }
@@ -300,7 +300,6 @@ impl Module<Button> for ClipboardModule {
}); });
} }
container.show_all();
hidden_option.hide(); hidden_option.hide();
Some(container) Some(container)

View File

@@ -63,7 +63,7 @@ impl Module<Button> for ClockModule {
let button = Button::new(); let button = Button::new();
let label = Label::new(None); let label = Label::new(None);
label.set_angle(info.bar_position.get_angle()); label.set_angle(info.bar_position.get_angle());
button.add(&label); button.append(&label);
let orientation = info.bar_position.get_orientation(); let orientation = info.bar_position.get_orientation();
button.connect_clicked(move |button| { button.connect_clicked(move |button| {
@@ -107,10 +107,10 @@ impl Module<Button> for ClockModule {
.build(); .build();
let format = "%H:%M:%S"; let format = "%H:%M:%S";
container.add(&clock); container.append(&clock);
let calendar = Calendar::builder().name("calendar").build(); let calendar = Calendar::builder().name("calendar").build();
container.add(&calendar); container.append(&calendar);
{ {
rx.attach(None, move |date| { rx.attach(None, move |date| {
@@ -120,8 +120,6 @@ impl Module<Button> for ClockModule {
}); });
} }
container.show_all();
Some(container) Some(container)
} }
} }

View File

@@ -23,7 +23,7 @@ impl CustomWidget for ButtonWidget {
if let Some(text) = self.label { if let Some(text) = self.label {
let label = Label::new(None); let label = Label::new(None);
label.set_use_markup(true); label.set_use_markup(true);
button.add(&label); button.append(&label);
DynamicString::new(&text, move |string| { DynamicString::new(&text, move |string| {
label.set_markup(&string); label.set_markup(&string);

View File

@@ -252,8 +252,6 @@ impl Module<gtk::Box> for CustomModule {
} }
} }
container.show_all();
Some(container) Some(container)
} }
} }

View File

@@ -8,6 +8,7 @@ use gtk::Scale;
use serde::Deserialize; use serde::Deserialize;
use std::cell::Cell; use std::cell::Cell;
use std::ops::Neg; use std::ops::Neg;
use glib::signal::Inhibit;
use tokio::spawn; use tokio::spawn;
use tracing::error; use tracing::error;

View File

@@ -104,8 +104,8 @@ impl Module<gtk::Box> for FocusedModule {
truncate.truncate_label(&label); truncate.truncate_label(&label);
} }
container.add(&icon); container.append(&icon);
container.add(&label); container.append(&label);
{ {
let icon_theme = icon_theme.clone(); let icon_theme = icon_theme.clone();

View File

@@ -10,6 +10,7 @@ use gtk::{Button, IconTheme, Orientation};
use indexmap::IndexMap; use indexmap::IndexMap;
use std::rc::Rc; use std::rc::Rc;
use std::sync::RwLock; use std::sync::RwLock;
use glib::signal::Inhibit;
use tokio::sync::mpsc::Sender; use tokio::sync::mpsc::Sender;
use tracing::error; use tracing::error;
@@ -234,8 +235,6 @@ impl ItemButton {
}); });
} }
button.show_all();
Self { Self {
button, button,
persistent: item.favorite, persistent: item.favorite,

View File

@@ -354,7 +354,7 @@ impl Module<gtk::Box> for LauncherModule {
&controller_tx, &controller_tx,
); );
container.add(&button.button); container.append(&button.button);
buttons.insert(item.app_id, button); buttons.insert(item.app_id, button);
} }
} }
@@ -433,7 +433,7 @@ impl Module<gtk::Box> for LauncherModule {
// we need some content to force the container to have a size // we need some content to force the container to have a size
let placeholder = Button::with_label("PLACEHOLDER"); let placeholder = Button::with_label("PLACEHOLDER");
placeholder.set_width_request(MAX_WIDTH); placeholder.set_width_request(MAX_WIDTH);
container.add(&placeholder); container.append(&placeholder);
let mut buttons = IndexMap::<String, IndexMap<usize, Button>>::new(); let mut buttons = IndexMap::<String, IndexMap<usize, Button>>::new();
@@ -525,10 +525,9 @@ impl Module<gtk::Box> for LauncherModule {
if let Some(buttons) = buttons.get(&app_id) { if let Some(buttons) = buttons.get(&app_id) {
for (_, button) in buttons { for (_, button) in buttons {
button.style_context().add_class("popup-item"); button.style_context().add_class("popup-item");
container.add(button); container.append(button);
} }
container.show_all();
container.set_width_request(MAX_WIDTH); container.set_width_request(MAX_WIDTH);
} }
} }

View File

@@ -257,7 +257,7 @@ pub fn wrap_widget<W: IsA<Widget>>(
let container = EventBox::new(); let container = EventBox::new();
container.add_events(EventMask::SCROLL_MASK); container.add_events(EventMask::SCROLL_MASK);
container.add(&revealer); container.append(&revealer);
common.install(&container, &revealer); common.install(&container, &revealer);

View File

@@ -13,6 +13,7 @@ use regex::Regex;
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
use glib::signal::Inhibit;
use tokio::spawn; use tokio::spawn;
use tokio::sync::mpsc::{Receiver, Sender}; use tokio::sync::mpsc::{Receiver, Sender};
use tracing::error; use tracing::error;
@@ -155,7 +156,7 @@ impl Module<Button> for MusicModule {
) -> Result<ModuleWidget<Button>> { ) -> Result<ModuleWidget<Button>> {
let button = Button::new(); let button = Button::new();
let button_contents = gtk::Box::new(Orientation::Horizontal, 5); let button_contents = gtk::Box::new(Orientation::Horizontal, 5);
button.add(&button_contents); button.append(&button_contents);
let icon_play = new_icon_label(&self.icons.play, info.icon_theme, self.icon_size); let icon_play = new_icon_label(&self.icons.play, info.icon_theme, self.icon_size);
let icon_pause = new_icon_label(&self.icons.pause, info.icon_theme, self.icon_size); let icon_pause = new_icon_label(&self.icons.pause, info.icon_theme, self.icon_size);
@@ -167,9 +168,9 @@ impl Module<Button> for MusicModule {
truncate.truncate_label(&label); truncate.truncate_label(&label);
} }
button_contents.add(&icon_pause); button_contents.append(&icon_pause);
button_contents.add(&icon_play); button_contents.append(&icon_play);
button_contents.add(&label); button_contents.append(&label);
let orientation = info.bar_position.get_orientation(); let orientation = info.bar_position.get_orientation();
@@ -261,9 +262,9 @@ impl Module<Button> for MusicModule {
album_label.container.set_widget_name("album"); album_label.container.set_widget_name("album");
artist_label.container.set_widget_name("artist"); artist_label.container.set_widget_name("artist");
info_box.add(&title_label.container); info_box.append(&title_label.container);
info_box.add(&album_label.container); info_box.append(&album_label.container);
info_box.add(&artist_label.container); info_box.append(&artist_label.container);
let controls_box = gtk::Box::builder().name("controls").build(); let controls_box = gtk::Box::builder().name("controls").build();
@@ -279,12 +280,12 @@ impl Module<Button> for MusicModule {
let btn_next = new_icon_button(&icons.next, icon_theme, self.icon_size); let btn_next = new_icon_button(&icons.next, icon_theme, self.icon_size);
btn_next.set_widget_name("btn-next"); btn_next.set_widget_name("btn-next");
controls_box.add(&btn_prev); controls_box.append(&btn_prev);
controls_box.add(&btn_play); controls_box.append(&btn_play);
controls_box.add(&btn_pause); controls_box.append(&btn_pause);
controls_box.add(&btn_next); controls_box.append(&btn_next);
info_box.add(&controls_box); info_box.append(&controls_box);
let volume_box = gtk::Box::builder() let volume_box = gtk::Box::builder()
.orientation(Orientation::Vertical) .orientation(Orientation::Vertical)
@@ -302,9 +303,9 @@ impl Module<Button> for MusicModule {
volume_box.pack_start(&volume_slider, true, true, 0); volume_box.pack_start(&volume_slider, true, true, 0);
volume_box.pack_end(&volume_icon, false, false, 0); volume_box.pack_end(&volume_icon, false, false, 0);
container.add(&album_image); container.append(&album_image);
container.add(&info_box); container.append(&info_box);
container.add(&volume_box); container.append(&volume_box);
let tx_prev = tx.clone(); let tx_prev = tx.clone();
btn_prev.connect_clicked(move |_| { btn_prev.connect_clicked(move |_| {
@@ -332,8 +333,6 @@ impl Module<Button> for MusicModule {
Inhibit(false) Inhibit(false)
}); });
container.show_all();
{ {
let icon_theme = icon_theme.clone(); let icon_theme = icon_theme.clone();
let image_size = self.cover_image_size; let image_size = self.cover_image_size;
@@ -464,8 +463,8 @@ impl IconLabel {
icon.style_context().add_class("icon"); icon.style_context().add_class("icon");
label.style_context().add_class("label"); label.style_context().add_class("label");
container.add(&icon); container.append(&icon);
container.add(&label); container.append(&label);
Self { label, container } Self { label, container }
} }

View File

@@ -199,7 +199,7 @@ impl Module<gtk::Box> for SysInfoModule {
.name("item") .name("item")
.build(); .build();
label.set_angle(info.bar_position.get_angle()); label.set_angle(info.bar_position.get_angle());
container.add(&label); container.append(&label);
labels.push(label); labels.push(label);
} }

View File

@@ -91,7 +91,7 @@ fn get_menu_items(
let menu = Menu::new(); let menu = Menu::new();
get_menu_items(&item_info.submenu, &tx.clone(), id, path) get_menu_items(&item_info.submenu, &tx.clone(), id, path)
.iter() .iter()
.for_each(|item| menu.add(item)); .for_each(|item| menu.append(item));
builder = builder.submenu(&menu); builder = builder.submenu(&menu);
} }
@@ -197,11 +197,11 @@ impl Module<MenuBar> for TrayModule {
}, },
|image| { |image| {
image.set_widget_name(address.as_str()); image.set_widget_name(address.as_str());
menu_item.add(&image); menu_item.append(&image);
}, },
); );
container.add(&menu_item); container.append(&menu_item);
menu_item.show_all(); menu_item.show_all();
menu_item menu_item
}); });
@@ -216,7 +216,7 @@ impl Module<MenuBar> for TrayModule {
&menu_path, &menu_path,
) )
.iter() .iter()
.for_each(|item| menu.add(item)); .for_each(|item| menu.append(item));
menu_item.set_submenu(Some(&menu)); menu_item.set_submenu(Some(&menu));
} }
} }

View File

@@ -186,7 +186,7 @@ impl Module<gtk::Box> for WorkspacesModule {
icon_size, icon_size,
&context.controller_tx, &context.controller_tx,
); );
container.add(&item); container.append(&item);
button_map.insert(workspace.name, item); button_map.insert(workspace.name, item);
} }
@@ -196,7 +196,6 @@ impl Module<gtk::Box> for WorkspacesModule {
reorder_workspaces(&container); reorder_workspaces(&container);
} }
container.show_all();
has_initialized = true; has_initialized = true;
} }
} }
@@ -223,7 +222,7 @@ impl Module<gtk::Box> for WorkspacesModule {
&context.controller_tx, &context.controller_tx,
); );
container.add(&item); container.append(&item);
if self.sort == SortOrder::Alphanumeric { if self.sort == SortOrder::Alphanumeric {
reorder_workspaces(&container); reorder_workspaces(&container);
} }
@@ -248,7 +247,7 @@ impl Module<gtk::Box> for WorkspacesModule {
&context.controller_tx, &context.controller_tx,
); );
container.add(&item); container.append(&item);
if self.sort == SortOrder::Alphanumeric { if self.sort == SortOrder::Alphanumeric {
reorder_workspaces(&container); reorder_workspaces(&container);

View File

@@ -1,4 +1,5 @@
use std::collections::HashMap; use std::collections::HashMap;
use glib::signal::Inhibit;
use crate::config::BarPosition; use crate::config::BarPosition;
use crate::modules::ModuleInfo; use crate::modules::ModuleInfo;
@@ -121,7 +122,7 @@ impl Popup {
if let Some(content) = self.cache.get(&key) { if let Some(content) = self.cache.get(&key) {
content.style_context().add_class("popup"); content.style_context().add_class("popup");
self.window.add(content); self.window.append(content);
} }
} }