Skip to content

Commit

Permalink
start porting to gtk4
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeStanger committed Feb 3, 2025
1 parent 03e6f10 commit 6a96f4e
Show file tree
Hide file tree
Showing 24 changed files with 780 additions and 809 deletions.
1,355 changes: 657 additions & 698 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ schema = ["dep:schemars"]

[dependencies]
# core
gtk = "0.18.2"
gtk-layer-shell = "0.8.2"
gtk = { package = "gtk4", version = "0.7.3" }
gtk-layer-shell = { package = "gtk4-layer-shell", version = "0.2.0" }
glib = "0.18.5"
tokio = { version = "1.43.0", features = [
"macros",
Expand Down
6 changes: 3 additions & 3 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@
rust
rust-analyzer-unwrapped
gcc
gtk3
gtk-layer-shell
gtk4
gt4-layer-shell
pkg-config
openssl
gdk-pixbuf
Expand All @@ -128,7 +128,7 @@
hicolor-icon-theme
gsettings-desktop-schemas
libxkbcommon
libdbusmenu-gtk3
# libdbusmenu-gtk3
libpulseaudio
libinput
libevdev
Expand Down
9 changes: 4 additions & 5 deletions src/bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use color_eyre::Result;
use glib::Propagation;
use gtk::gdk::Monitor;
use gtk::prelude::*;
use gtk::{Application, ApplicationWindow, IconTheme, Orientation, Window, WindowType};
use gtk::{Application, ApplicationWindow, IconTheme, Orientation, Window};
use gtk_layer_shell::LayerShell;
use std::rc::Rc;
use std::time::Duration;
Expand Down Expand Up @@ -48,7 +48,6 @@ impl Bar {
) -> Self {
let window = ApplicationWindow::builder()
.application(app)
.type_(WindowType::Toplevel)
.build();

let name = config
Expand Down Expand Up @@ -84,8 +83,8 @@ impl Bar {

window.connect_destroy_event(|_, _| {
info!("Shutting down");
gtk::main_quit();
Propagation::Proceed
// gtk::main_quit();
Propagation::Proceed
});

Self {
Expand Down Expand Up @@ -133,7 +132,7 @@ impl Bar {
);

if let Some(autohide) = config.autohide {
let hotspot_window = Window::new(WindowType::Toplevel);
let hotspot_window = Window::new();
Self::setup_autohide(&self.window, &hotspot_window, autohide);
self.setup_layer_shell(
&hotspot_window,
Expand Down
24 changes: 12 additions & 12 deletions src/config/common.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::dynamic_value::{dynamic_string, DynamicBool};
use crate::script::{Script, ScriptInput};
use glib::Propagation;
use gtk::gdk::ScrollDirection;
use gtk::prelude::*;
use gtk::{EventBox, Justification, Orientation, Revealer, RevealerTransitionType};
use gtk::{GestureClick, Justification, Orientation, Revealer, RevealerTransitionType, Widget};
use gtk::gdk::ScrollDirection;
use serde::Deserialize;
use tracing::trace;

Expand Down Expand Up @@ -238,14 +238,16 @@ impl TransitionType {

impl CommonConfig {
/// Configures the module's container according to the common config options.
pub fn install_events(mut self, container: &EventBox, revealer: &Revealer) {
pub fn install_events(mut self, container: &gtk::Box, revealer: &Revealer) {
self.install_show_if(container, revealer);

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 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() {
1 => left_click_script.as_ref(),
2 => middle_click_script.as_ref(),
Expand All @@ -257,8 +259,6 @@ impl CommonConfig {
trace!("Running on-click script: {}", event.button());
script.run_as_oneshot(None);
}

Propagation::Proceed
});

let scroll_up_script = self.on_scroll_up.map(Script::new_polling);
Expand Down Expand Up @@ -308,29 +308,29 @@ 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(
|| {
container.show_all();
widget.set_visible(true)
},
|show_if| {
let container = container.clone();
let widget = widget.clone();

{
let revealer = revealer.clone();
let container = container.clone();
let widget = widget.clone();

show_if.subscribe(move |success| {
if success {
container.show_all();
widget.show_all();
}
revealer.set_reveal_child(success);
});
}

revealer.connect_child_revealed_notify(move |revealer| {
if !revealer.reveals_child() {
container.hide();
widget.hide();
}
});
},
Expand Down
8 changes: 4 additions & 4 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ use crate::modules::script::ScriptModule;
use crate::modules::sway::mode::SwayModeModule;
#[cfg(feature = "sys_info")]
use crate::modules::sysinfo::SysInfoModule;
#[cfg(feature = "tray")]
use crate::modules::tray::TrayModule;
// #[cfg(feature = "tray")]
// use crate::modules::tray::TrayModule;
#[cfg(feature = "upower")]
use crate::modules::upower::UpowerModule;
#[cfg(feature = "volume")]
Expand Down Expand Up @@ -75,10 +75,10 @@ pub enum ModuleConfig {
Script(Box<ScriptModule>),
#[cfg(feature = "sys_info")]
SysInfo(Box<SysInfoModule>),
#[cfg(feature = "sway")]
// #[cfg(feature = "sway")]
SwayMode(Box<SwayModeModule>),
#[cfg(feature = "tray")]
Tray(Box<TrayModule>),
// Tray(Box<TrayModule>),
#[cfg(feature = "upower")]
Upower(Box<UpowerModule>),
#[cfg(feature = "volume")]
Expand Down
49 changes: 45 additions & 4 deletions src/gtk_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use gtk::{Label, Orientation, Widget};
pub struct WidgetGeometry {
/// Position of the start edge of the widget
/// from the start edge of the bar.
pub position: i32,
pub position: f64,
/// The length of the widget.
pub size: i32,
/// The length of the bar.
Expand All @@ -29,6 +29,8 @@ pub trait IronbarGtkExt {
fn get_tag<V: 'static>(&self, key: &str) -> Option<&V>;
/// Sets a data tag on a widget.
fn set_tag<V: 'static>(&self, key: &str, value: V);

fn children(&self) -> Vec<Box<dyn AsRef<Widget>>>;
}

impl<W: IsA<Widget>> IronbarGtkExt for W {
Expand All @@ -49,7 +51,7 @@ impl<W: IsA<Widget>> IronbarGtkExt for W {
allocation.height()
};

let top_level = self.toplevel().expect("Failed to get top-level widget");
let top_level = self.root().expect("Failed to get root widget");
let top_level_allocation = top_level.allocation();

let bar_size = if orientation == Orientation::Horizontal {
Expand All @@ -59,8 +61,8 @@ impl<W: IsA<Widget>> IronbarGtkExt for W {
};

let (widget_x, widget_y) = self
.translate_coordinates(&top_level, 0, 0)
.unwrap_or((0, 0));
.translate_coordinates(&top_level, 0.0, 0.0)
.unwrap_or((0.0, 0.0));

let widget_pos = if orientation == Orientation::Horizontal {
widget_x
Expand All @@ -82,8 +84,47 @@ impl<W: IsA<Widget>> IronbarGtkExt for W {
fn set_tag<V: 'static>(&self, key: &str, value: V) {
unsafe { self.set_data(key, value) }
}

fn children(&self) -> Vec<Box<dyn AsRef<Widget>>> {
let mut widget = self.first_child();
let mut children = vec![];

while let Some(w) = widget {
children.push(Box::new(w));
widget = w.next_sibling();
}

children
}
}

// struct IteratorWrapper<W: IsA<Widget>>(W);
//
// impl<W> Iterator for IteratorWrapper<W> {
// type Item = Box<dyn AsRef<Widget>>;
//
// fn next(&mut self) -> Option<Self::Item> {
// self.0
// }
// }
//
// struct IntoIter<W: IsA<Widget>> {
// widget: W,
// next: Option<Box<dyn AsRef<Widget>>>
// }
//
// impl<W: IsA<Widget>> IntoIterator for IteratorWrapper<W> {
// type Item = Box<dyn AsRef<Widget>>;
// type IntoIter = IntoIter<Self>;
//
// fn into_iter(self) -> Self::IntoIter {
// IntoIter {
// widget: self,
// next: self.first_child().map(Box::new)
// }
// }
// }

pub trait IronbarLabelExt {
/// Sets the label value to the provided string.
///
Expand Down
2 changes: 1 addition & 1 deletion src/image/gtk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl IconLabel {
image.add_class("icon");
image.add_class("image");

container.add(&image);
container.append(&image);
container.add(&label);

if ImageProvider::is_definitely_image_input(input) {
Expand Down
3 changes: 2 additions & 1 deletion src/image/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ use crate::{glib_recv_mpsc, send_async, spawn};
use cfg_if::cfg_if;
use color_eyre::{Help, Report, Result};
use gtk::cairo::Surface;
use gtk::gdk::ffi::gdk_cairo_surface_create_from_pixbuf;
use gtk::gdk_pixbuf::Pixbuf;
use gtk::prelude::*;
use gtk::{IconLookupFlags, IconTheme};
use std::path::{Path, PathBuf};
use gtk::gdk::ffi::gdk_texture_new_for_pixbuf;
use gtk::gdk::Texture;
#[cfg(feature = "http")]
use tokio::sync::mpsc;
use tracing::{debug, warn};
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ fn load_config() -> (Config, PathBuf) {
}

/// Gets the GDK `Display` instance.
fn get_display() -> Display {
pub fn get_display() -> Display {
Display::default().map_or_else(
|| {
let report = Report::msg("Failed to get default GTK display");
Expand Down
31 changes: 13 additions & 18 deletions src/modules/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use glib::Propagation;
use gtk::gdk_pixbuf::Pixbuf;
use gtk::gio::{Cancellable, MemoryInputStream};
use gtk::prelude::*;
use gtk::{Button, EventBox, Image, Label, Orientation, RadioButton, Widget};
use gtk::{Button, Image, Label, Orientation, CheckButton, Widget};
use serde::Deserialize;
use std::collections::HashMap;
use tokio::sync::{broadcast, mpsc};
Expand Down Expand Up @@ -171,10 +171,10 @@ impl Module<Button> for ClipboardModule {
let container = gtk::Box::new(Orientation::Vertical, 10);

let entries = gtk::Box::new(Orientation::Vertical, 5);
container.add(&entries);
container.append(&entries);

let hidden_option = RadioButton::new();
entries.add(&hidden_option);
let hidden_option = CheckButton::new();
entries.append(&hidden_option);

let mut items = HashMap::new();

Expand All @@ -190,10 +190,10 @@ impl Module<Button> for ClipboardModule {

let button = match item.value.as_ref() {
ClipboardValue::Text(value) => {
let button = RadioButton::from_widget(&hidden_option);
let button = CheckButton::from_widget(&hidden_option);

let label = Label::new(Some(value));
button.add(&label);
button.append(&label);

if let Some(truncate) = self.truncate {
label.truncate(truncate);
Expand All @@ -216,7 +216,7 @@ impl Module<Button> for ClipboardModule {
Ok(pixbuf) => {
let image = Image::from_pixbuf(Some(&pixbuf));

let button = RadioButton::from_widget(&hidden_option);
let button = CheckButton::from_widget(&hidden_option);
button.set_image(Some(&image));
button.set_always_show_image(true);
button.style_context().add_class("image");
Expand All @@ -235,15 +235,12 @@ impl Module<Button> for ClipboardModule {
button.style_context().add_class("btn");
button.set_active(true); // if just added, should be on clipboard

let button_wrapper = EventBox::new();
button_wrapper.add(&button);

button_wrapper.set_widget_name(&format!("copy-{id}"));
button_wrapper.set_above_child(true);
button.set_widget_name(&format!("copy-{id}"));
button.set_above_child(true);

{
let tx = tx.clone();
button_wrapper.connect_button_press_event(
button.connect_button_press_event(
move |button_wrapper, event| {
// left click
if event.button() == 1 {
Expand Down Expand Up @@ -279,12 +276,11 @@ impl Module<Button> for ClipboardModule {
});
}

row.add(&button_wrapper);
row.pack_end(&remove_button, false, false, 0);
row.append(&button);
row.pack_end(&remove_button, false);

entries.add(&row);
entries.append(&row);
entries.reorder_child(&row, 0);
row.show_all();

items.insert(id, (row, button));
}
Expand Down Expand Up @@ -316,7 +312,6 @@ impl Module<Button> for ClipboardModule {
});
}

container.show_all();
hidden_option.hide();

Some(container)
Expand Down
6 changes: 2 additions & 4 deletions src/modules/clock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,11 @@ impl Module<Button> for ClockModule {
.build();
clock.add_class("calendar-clock");

container.add(&clock);
container.append(&clock);

let calendar = Calendar::new();
calendar.add_class("calendar");
container.add(&calendar);
container.append(&calendar);

let format = self.format_popup;
let locale = Locale::try_from(self.locale.as_str()).unwrap_or(Locale::POSIX);
Expand All @@ -197,8 +197,6 @@ impl Module<Button> for ClockModule {
clock.set_label(&date_string);
});

container.show_all();

Some(container)
}
}
Loading

0 comments on commit 6a96f4e

Please sign in to comment.