Skip to content

Commit

Permalink
finish port to libadwaita dialogs
Browse files Browse the repository at this point in the history
  • Loading branch information
ranfdev committed Feb 13, 2024
1 parent 29377dc commit 86d1472
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 59 deletions.
2 changes: 1 addition & 1 deletion data/resources/ui/preferences.blp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Gtk 4.0;
using Adw 1;

template $NotifyPreferences : Adw.PreferencesWindow {
template $NotifyPreferences : Adw.PreferencesDialog {
width-request: 240;
height-request: 360;
Adw.PreferencesPage {
Expand Down
11 changes: 4 additions & 7 deletions src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use std::path::PathBuf;
use std::pin::Pin;
use std::rc::Rc;

use adw::prelude::*;
use adw::subclass::prelude::*;
use capnp_rpc::{rpc_twoparty_capnp, twoparty, RpcSystem};
use futures::stream::Stream;
use futures::AsyncReadExt;
use gio::SocketClient;
use gio::UnixSocketAddress;
use gtk::prelude::*;
use gtk::{gdk, gio, glib};
use ntfy_daemon::models;
use ntfy_daemon::ntfy_capnp::system_notifier;
Expand Down Expand Up @@ -217,23 +217,20 @@ impl NotifyApplication {
}

fn show_about_dialog(&self) {
let dialog = adw::AboutWindow::from_appdata(
let dialog = adw::AboutDialog::from_appdata(
"/com/ranfdev/Notify/com.ranfdev.Notify.metainfo.xml",
None,
);
if let Some(w) = self.imp().window.borrow().upgrade() {
dialog.set_transient_for(Some(&w));
dialog.present(&w);
}

dialog.present();
}

fn show_preferences(&self) {
let win = crate::widgets::NotifyPreferences::new(
self.main_window().imp().notifier.get().unwrap().clone(),
);
win.set_transient_for(Some(&self.main_window()));
win.present();
win.present(&self.main_window());
}

pub fn run(&self) -> glib::ExitCode {
Expand Down
5 changes: 0 additions & 5 deletions src/widgets/add_subscription_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ mod imp {
type ParentType = adw::Dialog;

fn class_init(klass: &mut Self::Class) {
klass.add_binding_action(
gtk::gdk::Key::Escape,
gtk::gdk::ModifierType::empty(),
"window.close",
);
klass.install_action("default.activate", None, |this, _, _| {
this.emit_subscribe_request();
});
Expand Down
56 changes: 24 additions & 32 deletions src/widgets/advanced_message_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,22 @@ mod imp {
impl ObjectSubclass for AdvancedMessageDialog {
const NAME: &'static str = "AdvancedMessageDialog";
type Type = super::AdvancedMessageDialog;
type ParentType = adw::Window;
type ParentType = adw::Dialog;
}

impl ObjectImpl for AdvancedMessageDialog {}
impl WidgetImpl for AdvancedMessageDialog {}
impl WindowImpl for AdvancedMessageDialog {}
impl AdwWindowImpl for AdvancedMessageDialog {}
impl AdwDialogImpl for AdvancedMessageDialog {}
}

glib::wrapper! {
pub struct AdvancedMessageDialog(ObjectSubclass<imp::AdvancedMessageDialog>)
@extends gtk::Widget, gtk::Window, adw::Window;
@extends gtk::Widget, adw::Dialog;
}

impl AdvancedMessageDialog {
pub fn new(
parent: &impl IsA<gtk::Window>,
subscription: Subscription,
message: String,
) -> Self {
pub fn new(subscription: Subscription, message: String) -> Self {
let this: Self = glib::Object::new();
this.set_transient_for(Some(parent));
this.set_modal(true);
this.set_default_height(400);
this.imp().subscription.set(subscription).unwrap();
this.build_ui(
this.imp().subscription.get().unwrap().topic().clone(),
Expand All @@ -52,14 +44,17 @@ impl AdvancedMessageDialog {
this
}
fn build_ui(&self, topic: String, message: String) {
self.set_title("Advanced Message");
self.set_content_height(480);
self.set_content_width(480);
let this = self.clone();
relm4_macros::view! {
content = &adw::ToolbarView {
add_top_bar = &adw::HeaderBar {},
#[wrap(Some)]
set_content: toast_overlay = &adw::ToastOverlay {
#[wrap(Some)]
set_child = &adw::Clamp {
set_child = &gtk::ScrolledWindow {
#[wrap(Some)]
set_child = &gtk::Box {
set_margin_top: 8,
Expand All @@ -82,22 +77,19 @@ impl AdvancedMessageDialog {
set_xalign: 0.0,
set_halign: gtk::Align::Start,
},
append = &gtk::ScrolledWindow {
#[wrap(Some)]
set_child: text_view = &gsv::View {
add_css_class: "code",
set_tab_width: 4,
set_indent_width: 2,
set_auto_indent: true,
set_top_margin: 4,
set_bottom_margin: 4,
set_left_margin: 4,
set_right_margin: 4,
set_hexpand: true,
set_vexpand: true,
set_monospace: true,
set_background_pattern: gsv::BackgroundPatternType::Grid
},
append: text_view = &gsv::View {
add_css_class: "code",
set_tab_width: 4,
set_indent_width: 2,
set_auto_indent: true,
set_top_margin: 4,
set_bottom_margin: 4,
set_left_margin: 4,
set_right_margin: 4,
set_hexpand: true,
set_vexpand: true,
set_monospace: true,
set_background_pattern: gsv::BackgroundPatternType::Grid
},
append = &gtk::Label {
add_css_class: "heading",
Expand Down Expand Up @@ -166,9 +158,9 @@ impl AdvancedMessageDialog {
add_css_class: "circular",
add_css_class: "small",
set_label: "?",
connect_clicked[this] => move |_| {
connect_clicked => move |_| {
gtk::UriLauncher::new("https://docs.ntfy.sh/publish/#publish-as-json").launch(
Some(&this),
None::<&gtk::Window>,
gio::Cancellable::NONE,
|_| {}
);
Expand Down Expand Up @@ -221,6 +213,6 @@ impl AdvancedMessageDialog {
};
let scheme = gsv::StyleSchemeManager::default().scheme(scheme_name);
buffer.set_style_scheme(scheme.as_ref());
this.set_content(Some(&content));
this.set_child(Some(&content));
}
}
11 changes: 4 additions & 7 deletions src/widgets/preferences.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ mod imp {
impl ObjectSubclass for NotifyPreferences {
const NAME: &'static str = "NotifyPreferences";
type Type = super::NotifyPreferences;
type ParentType = adw::PreferencesWindow;
type ParentType = adw::PreferencesDialog;

fn class_init(klass: &mut Self::Class) {
klass.bind_template();
Expand All @@ -66,16 +66,13 @@ mod imp {
}

impl WidgetImpl for NotifyPreferences {}
impl WindowImpl for NotifyPreferences {}

impl ApplicationWindowImpl for NotifyPreferences {}
impl AdwWindowImpl for NotifyPreferences {}
impl PreferencesWindowImpl for NotifyPreferences {}
impl AdwDialogImpl for NotifyPreferences {}
impl PreferencesDialogImpl for NotifyPreferences {}
}

glib::wrapper! {
pub struct NotifyPreferences(ObjectSubclass<imp::NotifyPreferences>)
@extends gtk::Widget, gtk::Window, adw::Window, adw::PreferencesWindow,
@extends gtk::Widget, adw::Dialog, adw::PreferencesDialog,
@implements gio::ActionMap, gio::ActionGroup, gtk::Root;
}

Expand Down
5 changes: 0 additions & 5 deletions src/widgets/subscription_info_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ mod imp {

fn class_init(klass: &mut Self::Class) {
klass.bind_template();
klass.add_binding_action(
gtk::gdk::Key::Escape,
gtk::gdk::ModifierType::empty(),
"window.close",
);
}

// You must call `Widget`'s `init_template()` within `instance_init()`.
Expand Down
3 changes: 1 addition & 2 deletions src/widgets/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,7 @@ impl NotifyWindow {
imp.code_btn.connect_clicked(move |_| {
let this = this.clone();
this.selected_subscription().map(move |sub| {
AdvancedMessageDialog::new(&this, sub, this.imp().entry.text().to_string())
.present()
AdvancedMessageDialog::new(sub, this.imp().entry.text().to_string()).present(&this)
});
});
}
Expand Down

0 comments on commit 86d1472

Please sign in to comment.