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 Apr 30, 2023
1 parent 0e3102d commit c735853
Show file tree
Hide file tree
Showing 19 changed files with 76 additions and 78 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ workspaces = ["futures-util"]

[dependencies]
# core
gtk = "0.17.0"
gtk-layer-shell = "0.6.0"
glib = "0.17.5"
gtk = { package = "gtk4", version = "0.6.6" }
gtk-layer-shell = { package = "gtk4-layer-shell", version = "0.0.3" }
glib = "0.17.9"
tokio = { version = "1.21.2", features = ["macros", "rt-multi-thread", "time", "process", "sync", "io-util", "net"] }
tracing = "0.1.37"
tracing-subscriber = { version = "0.3.16", features = ["env-filter"] }
Expand Down
9 changes: 5 additions & 4 deletions src/bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use gtk::gdk::Monitor;
use gtk::prelude::*;
use gtk::{Application, ApplicationWindow, IconTheme, Orientation};
use std::sync::{Arc, RwLock};
use glib::signal::Inhibit;
use tracing::{debug, info};

/// Creates a new window for a bar,
Expand Down Expand Up @@ -48,16 +49,16 @@ pub fn create_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);

load_modules(&start, &center, &end, app, config, monitor, monitor_name)?;
win.add(&content);
win.append(&content);

win.connect_destroy_event(|_, _| {
info!("Shutting down");
gtk::main_quit();
// gtk::main_quit();
Inhibit(false)
});

Expand Down Expand Up @@ -197,7 +198,7 @@ fn add_modules(
let common = $module.common.take().expect("Common config did not exist");
let widget = create_module(*$module, $id, &info, &Arc::clone(&popup))?;
let container = wrap_widget(&widget, common, orientation);
content.add(&container);
content.append(&container);
}};
}

Expand Down
25 changes: 13 additions & 12 deletions src/config/common.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use glib::signal::Inhibit;
use crate::dynamic_string::DynamicString;
use crate::script::{Script, ScriptInput};
use crate::send;
use gtk::gdk::ScrollDirection;
use gtk::prelude::*;
use gtk::{EventBox, Orientation, Revealer, RevealerTransitionType};
use gtk::{GestureClick, Orientation, Revealer, RevealerTransitionType, Widget};
use serde::Deserialize;
use tokio::spawn;
use tracing::trace;
Expand Down Expand Up @@ -55,14 +56,16 @@ impl TransitionType {

impl CommonConfig {
/// Configures the module's container according to the common config options.
pub fn install(mut self, container: &EventBox, revealer: &Revealer) {
self.install_show_if(container, revealer);
pub fn install<W: IsA<Widget>>(mut self, widget: &W, revealer: &Revealer) {
self.install_show_if(widget, 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 @@ -74,14 +77,12 @@ impl CommonConfig {
trace!("Running on-click script: {}", event.button());
script.run_as_oneshot(None);
}

Inhibit(false)
});

let scroll_up_script = self.on_scroll_up.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() {
ScrollDirection::Up => scroll_up_script.as_ref(),
ScrollDirection::Down => scroll_down_script.as_ref(),
Expand All @@ -99,7 +100,7 @@ impl CommonConfig {
macro_rules! install_oneshot {
($option:expr, $method:ident) => {
$option.map(Script::new_polling).map(|script| {
container.$method(move |_, _| {
widget.$method(move |_, _| {
script.run_as_oneshot(None);
Inhibit(false)
});
Expand All @@ -111,22 +112,22 @@ impl CommonConfig {
install_oneshot!(self.on_mouse_exit, connect_leave_notify_event);

if let Some(tooltip) = self.tooltip {
let container = container.clone();
let container = widget.clone();
DynamicString::new(&tooltip, move |string| {
container.set_tooltip_text(Some(&string));
Continue(true)
});
}
}

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 script = Script::new_polling(show_if);
let container = container.clone();
let widget = widget.clone();
let (tx, rx) = glib::MainContext::channel(glib::PRIORITY_DEFAULT);

spawn(async move {
Expand Down
4 changes: 2 additions & 2 deletions src/image/gtk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub fn new_icon_label(input: &str, icon_theme: &IconTheme, size: i32) -> gtk::Bo
let image = Image::new();
image.set_widget_name("image");

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

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

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

container
Expand Down
7 changes: 5 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::style::load_css;
use color_eyre::eyre::Result;
use color_eyre::Report;
use dirs::config_dir;
use gtk::gdk::Display;
use gtk::gdk::{Display, Monitor};
use gtk::prelude::*;
use gtk::Application;
use std::env;
Expand Down Expand Up @@ -120,7 +120,10 @@ fn create_bars(
debug!("Received {} outputs from Wayland", outputs.len());
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 {
let monitor = display
Expand Down
17 changes: 8 additions & 9 deletions src/modules/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ use crate::try_send;
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, RadioButton, Widget};
use serde::Deserialize;
use std::collections::HashMap;
use std::sync::Arc;
use glib::signal::Inhibit;
use tokio::spawn;
use tokio::sync::mpsc::{Receiver, Sender};
use tracing::{debug, error};
Expand Down Expand Up @@ -161,10 +162,10 @@ impl Module<Button> for ClipboardModule {
.build();

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

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

let mut items = HashMap::new();

Expand All @@ -183,7 +184,7 @@ impl Module<Button> for ClipboardModule {
let button = RadioButton::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 Down Expand Up @@ -218,7 +219,7 @@ impl Module<Button> for ClipboardModule {
button.set_active(true); // if just added, should be on clipboard

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

button_wrapper.set_widget_name(&format!("copy-{id}"));
button_wrapper.set_above_child(true);
Expand Down Expand Up @@ -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);

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

items.insert(id, (row, button));
}
Expand Down Expand Up @@ -300,7 +300,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 @@ -63,7 +63,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 orientation = info.bar_position.get_orientation();
button.connect_clicked(move |button| {
Expand Down Expand Up @@ -107,10 +107,10 @@ impl Module<Button> for ClockModule {
.build();
let format = "%H:%M:%S";

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

let calendar = Calendar::builder().name("calendar").build();
container.add(&calendar);
container.append(&calendar);

{
rx.attach(None, move |date| {
Expand All @@ -120,8 +120,6 @@ impl Module<Button> for ClockModule {
});
}

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 @@ -23,7 +23,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);

DynamicString::new(&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 @@ -252,8 +252,6 @@ impl Module<gtk::Box> for CustomModule {
}
}

container.show_all();

Some(container)
}
}
1 change: 1 addition & 0 deletions src/modules/custom/slider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use gtk::Scale;
use serde::Deserialize;
use std::cell::Cell;
use std::ops::Neg;
use glib::signal::Inhibit;
use tokio::spawn;
use tracing::error;

Expand Down
4 changes: 2 additions & 2 deletions src/modules/focused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ impl Module<gtk::Box> for FocusedModule {
truncate.truncate_label(&label);
}

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

{
let icon_theme = icon_theme.clone();
Expand Down
3 changes: 1 addition & 2 deletions src/modules/launcher/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use gtk::{Button, IconTheme, Orientation};
use indexmap::IndexMap;
use std::rc::Rc;
use std::sync::RwLock;
use glib::signal::Inhibit;
use tokio::sync::mpsc::Sender;
use tracing::error;

Expand Down Expand Up @@ -234,8 +235,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 @@ -354,7 +354,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 @@ -433,7 +433,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 @@ -525,10 +525,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
2 changes: 1 addition & 1 deletion src/modules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ pub fn wrap_widget<W: IsA<Widget>>(

let container = EventBox::new();
container.add_events(EventMask::SCROLL_MASK);
container.add(&revealer);
container.append(&revealer);

common.install(&container, &revealer);

Expand Down
Loading

0 comments on commit c735853

Please sign in to comment.