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 Jan 14, 2024
1 parent e737177 commit 5bae1a9
Show file tree
Hide file tree
Showing 22 changed files with 847 additions and 856 deletions.
1,448 changes: 727 additions & 721 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 @@ -63,8 +63,8 @@ workspaces = ["futures-util"]

[dependencies]
# core
gtk = "0.18.1"
gtk-layer-shell = "0.8.0"
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.35.1", features = [
"macros",
Expand Down
15 changes: 7 additions & 8 deletions src/bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,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::cell::RefCell;
use std::rc::Rc;
Expand Down Expand Up @@ -49,7 +49,6 @@ impl Bar {
) -> Self {
let window = ApplicationWindow::builder()
.application(app)
.type_(WindowType::Toplevel)
.build();

let name = config
Expand Down Expand Up @@ -81,16 +80,16 @@ impl Bar {
let center = create_container("center", orientation);
let end = create_container("end", orientation);

content.add(&start);
content.append(&start);
content.set_center_widget(Some(&center));
content.pack_end(&end, false, false, 0);

window.add(&content);
window.append(&content);

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

Bar {
Expand Down Expand Up @@ -134,7 +133,7 @@ impl Bar {
let start_hidden = config.start_hidden.unwrap_or(config.autohide.is_some());

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(
Expand Down Expand Up @@ -365,7 +364,7 @@ fn add_modules(
set_widget_identifiers(&widget_parts, &common);

let container = wrap_widget(&widget_parts.widget, common, orientation);
content.add(&container);
content.append(&container);
}};
}

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, Orientation, Revealer, RevealerTransitionType};
use gtk::{GestureClick, Orientation, Revealer, RevealerTransitionType, Widget};
use gtk::gdk::ScrollDirection;
use serde::Deserialize;
use tracing::trace;

Expand Down Expand Up @@ -56,14 +56,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 @@ -75,8 +77,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 @@ -119,29 +119,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
1 change: 0 additions & 1 deletion src/config/truncate.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use gtk::pango::EllipsizeMode as GtkEllipsizeMode;
use gtk::prelude::*;
use serde::Deserialize;

#[derive(Debug, Deserialize, Clone, Copy)]
Expand Down
23 changes: 23 additions & 0 deletions src/gtk_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,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);
}

impl<W: IsA<Widget>> IronbarGtkExt for W {
Expand Down Expand Up @@ -74,4 +76,25 @@ 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) {
self.first
}
}

struct IntoIter<W: IsA<Widget>> {
widget: W,
next: Option<dyn IsA<Widget>>
}

impl<W: IsA<Widget>> IntoIterator for &W {
type Item = dyn IsA<Widget>;
type IntoIter = IntoIter<Self>;

fn into_iter(self) -> Self::IntoIter {
IntoIter {
widget: self,
next: self.first_child()
}
}
}
4 changes: 2 additions & 2 deletions src/image/gtk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub fn new_icon_label(input: &str, icon_theme: &IconTheme, size: i32) -> gtk::Bo
image.add_class("icon");
image.add_class("image");

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

ImageProvider::parse(input, icon_theme, false, size)
.map(|provider| provider.load_into_image(image));
Expand All @@ -48,7 +48,7 @@ pub fn new_icon_label(input: &str, icon_theme: &IconTheme, size: i32) -> gtk::Bo
label.add_class("icon");
label.add_class("text-icon");

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

container
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ fn load_config() -> Config {
}

/// 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 @@ -10,7 +10,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 std::sync::Arc;
Expand Down Expand Up @@ -155,10 +155,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 @@ -174,10 +174,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 {
truncate.truncate_label(&label);
Expand All @@ -198,7 +198,7 @@ impl Module<Button> for ClipboardModule {
.expect("Failed to read Pixbuf from stream");
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 @@ -211,15 +211,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 @@ -255,12 +252,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 @@ -292,7 +288,6 @@ impl Module<Button> for ClipboardModule {
});
}

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

Some(container)
Expand Down
8 changes: 3 additions & 5 deletions src/modules/clock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl Module<Button> for ClockModule {
let button = Button::new();
let label = Label::new(None);
label.set_angle(info.bar_position.get_angle());
button.add(&label);
button.append(&label);

let tx = context.tx.clone();
button.connect_clicked(move |button| {
Expand Down Expand Up @@ -135,11 +135,11 @@ impl Module<Button> for ClockModule {
let clock = Label::builder().halign(Align::Center).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 @@ -149,8 +149,6 @@ impl Module<Button> for ClockModule {
clock.set_label(&date_string);
});

container.show_all();

Some(container)
}
}
2 changes: 1 addition & 1 deletion src/modules/custom/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl CustomWidget for ButtonWidget {
if let Some(text) = self.label {
let label = Label::new(None);
label.set_use_markup(true);
button.add(&label);
button.append(&label);

dynamic_string(&text, move |string| {
label.set_markup(&string);
Expand Down
2 changes: 0 additions & 2 deletions src/modules/custom/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,6 @@ impl Module<gtk::Box> for CustomModule {
}
}

container.show_all();

Some(container)
}
}
1 change: 0 additions & 1 deletion src/modules/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::dynamic_value::dynamic_string;
use crate::modules::{Module, ModuleInfo, ModuleParts, ModuleUpdateEvent, WidgetContext};
use crate::{glib_recv, try_send};
use color_eyre::Result;
use gtk::prelude::*;
use gtk::Label;
use serde::Deserialize;
use tokio::sync::mpsc;
Expand Down
2 changes: 0 additions & 2 deletions src/modules/launcher/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,6 @@ impl ItemButton {
});
}

button.show_all();

Self {
button,
persistent: item.favorite,
Expand Down
7 changes: 3 additions & 4 deletions src/modules/launcher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ impl Module<gtk::Box> for LauncherModule {
&controller_tx,
);

container.add(&button.button);
container.append(&button.button);
buttons.insert(item.app_id, button);
}
}
Expand Down Expand Up @@ -423,7 +423,7 @@ impl Module<gtk::Box> for LauncherModule {
// we need some content to force the container to have a size
let placeholder = Button::with_label("PLACEHOLDER");
placeholder.set_width_request(MAX_WIDTH);
container.add(&placeholder);
container.append(&placeholder);

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

Expand Down Expand Up @@ -507,10 +507,9 @@ impl Module<gtk::Box> for LauncherModule {
if let Some(buttons) = buttons.get(&app_id) {
for (_, button) in buttons {
button.style_context().add_class("popup-item");
container.add(button);
container.append(button);
}

container.show_all();
container.set_width_request(MAX_WIDTH);
}
}
Expand Down
Loading

0 comments on commit 5bae1a9

Please sign in to comment.