Skip to content

Commit 975417f

Browse files
committed
use relm4::view!
1 parent be6ce20 commit 975417f

File tree

4 files changed

+94
-108
lines changed

4 files changed

+94
-108
lines changed

Cargo.lock

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ rand = "0.8.5"
3030
ureq = "2.7.1"
3131
futures = "0.3.0"
3232
ashpd = "0.6.0"
33+
relm4-macros = { version = "0.6.2", features = [], default-features = false }

src/application.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use gtk::prelude::*;
1010
use gtk::{gdk, gio, glib};
1111
use ntfy_daemon::models;
1212
use ntfy_daemon::ntfy_capnp::system_notifier;
13-
use tracing::{debug, info};
13+
use tracing::{debug, info, warn};
1414

1515
use crate::config::{APP_ID, PKGDATADIR, PROFILE, VERSION};
1616
use crate::widgets::*;
@@ -69,7 +69,9 @@ mod imp {
6969
}
7070

7171
glib::MainContext::default().spawn_local(async move {
72-
super::NotifyApplication::run_in_background().await.unwrap();
72+
if let Err(e) = super::NotifyApplication::run_in_background().await {
73+
warn!(error = %e, "couldn't request running in background from portal");
74+
}
7375
});
7476

7577
if is_daemon {

src/widgets/add_subscription_dialog.rs

Lines changed: 77 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1+
use std::cell::RefCell;
2+
13
use adw::prelude::*;
24
use adw::subclass::prelude::*;
35
use glib::once_cell::sync::Lazy;
46
use glib::subclass::Signal;
5-
use glib::Properties;
67
use gtk::gio;
78
use gtk::glib;
89

910
mod imp {
1011
pub use super::*;
11-
#[derive(Debug, Default, Properties)]
12-
#[properties(wrapper_type = super::AddSubscriptionDialog)]
12+
#[derive(Debug, Default)]
1313
pub struct AddSubscriptionDialog {
14-
#[property(name = "topic", get = |imp: &Self| imp.topic_entry.text(), type = glib::GString)]
15-
pub topic_entry: adw::EntryRow,
16-
#[property(name = "server", get = |imp: &Self| imp.server_entry.text(), type = glib::GString)]
17-
pub server_entry: adw::EntryRow,
14+
pub topic_entry: RefCell<adw::EntryRow>,
15+
pub server_entry: RefCell<adw::EntryRow>,
1816
}
1917

2018
#[glib::object_subclass]
@@ -36,7 +34,7 @@ mod imp {
3634
});
3735
}
3836
}
39-
#[glib::derived_properties]
37+
4038
impl ObjectImpl for AddSubscriptionDialog {
4139
fn signals() -> &'static [Signal] {
4240
static SIGNALS: Lazy<Vec<Signal>> =
@@ -72,108 +70,81 @@ impl AddSubscriptionDialog {
7270
obj.set_modal(true);
7371
obj.set_default_width(360);
7472

75-
let toolbar_view = adw::ToolbarView::new();
76-
toolbar_view.add_top_bar(&adw::HeaderBar::new());
77-
78-
let content = gtk::Box::builder()
79-
.orientation(gtk::Orientation::Vertical)
80-
.spacing(12)
81-
.margin_end(12)
82-
.margin_start(12)
83-
.margin_top(12)
84-
.margin_bottom(12)
85-
.build();
86-
let clamp = adw::Clamp::new();
87-
clamp.set_child(Some(&content));
88-
89-
let description = {
90-
let d = gtk::Label::builder()
91-
.label("Topics may not be password-protected, so choose a name that's not easy to guess. Once subscribed, you can PUT/POST notifications.")
92-
.wrap(true)
93-
.xalign(0.0)
94-
.wrap_mode(gtk::pango::WrapMode::WordChar)
95-
.build();
96-
d.add_css_class("dim-label");
97-
d
98-
};
99-
100-
content.append(&description);
101-
102-
let topic_entry = {
103-
let e = &imp.topic_entry;
104-
e.set_title("Topic");
105-
e.set_activates_default(true);
106-
107-
let rand_btn = {
108-
let b = gtk::Button::builder()
109-
.icon_name("dice3-symbolic")
110-
.tooltip_text("Generate Name")
111-
.valign(gtk::Align::Center)
112-
.css_classes(["flat"])
113-
.build();
114-
let ec = e.clone();
115-
b.connect_clicked(move |_| {
116-
use rand::distributions::Alphanumeric;
117-
use rand::{thread_rng, Rng};
118-
let mut rng = thread_rng();
119-
let chars: String = (0..10).map(|_| rng.sample(Alphanumeric) as char).collect();
120-
ec.set_text(&chars);
121-
});
122-
b
123-
};
124-
125-
e.add_suffix(&rand_btn);
126-
e
127-
};
128-
// TODO: Reserved topics
129-
/*let reserved_switch = {
130-
adw::SwitchRow::builder()
131-
.title("Reserved")
132-
.subtitle("For Ntfy Pro users only")
133-
.build()
134-
};*/
135-
let server_entry = &imp.server_entry;
136-
server_entry.set_title("Server");
137-
138-
let expander_row = {
139-
let e = adw::ExpanderRow::builder()
140-
.title("Custom Server...")
141-
.enable_expansion(false)
142-
.show_enable_switch(true)
143-
.build();
144-
e.add_row(server_entry);
145-
e
146-
};
147-
let list_box = {
148-
let l = gtk::ListBox::new();
149-
l.add_css_class("boxed-list");
150-
l.append(topic_entry);
151-
// l.append(&reserved_switch);
152-
l.append(&expander_row);
153-
l
154-
};
155-
content.append(&list_box);
156-
157-
let sub_btn = {
158-
let b = gtk::Button::new();
159-
b.set_label("Subscribe");
160-
b.add_css_class("suggested-action");
161-
b.add_css_class("pill");
162-
b.set_halign(gtk::Align::Center);
163-
164-
let wc = obj.clone();
165-
b.connect_clicked(move |_| {
166-
wc.emit_subscribe_request();
167-
wc.close();
168-
});
169-
b
170-
};
73+
relm4_macros::view! {
74+
toolbar_view = adw::ToolbarView {
75+
add_top_bar: &adw::HeaderBar::new(),
76+
#[wrap(Some)]
77+
set_content = &adw::Clamp {
78+
#[wrap(Some)]
79+
set_child = &gtk::Box {
80+
set_orientation: gtk::Orientation::Vertical,
81+
set_spacing: 12,
82+
set_margin_end: 12,
83+
set_margin_start: 12,
84+
set_margin_top: 12,
85+
set_margin_bottom: 12,
86+
append = &gtk::Label {
87+
add_css_class: "dim-label",
88+
set_label: "Topics may not be password-protected, so choose a name that's not easy to guess. \
89+
Once subscribed, you can PUT/POST notifications.",
90+
set_wrap: true,
91+
set_xalign: 0.0,
92+
set_wrap_mode: gtk::pango::WrapMode::WordChar
93+
},
94+
append = &gtk::ListBox {
95+
add_css_class: "boxed-list",
96+
append: topic_entry = &adw::EntryRow {
97+
set_title: "Topic",
98+
set_activates_default: true,
99+
add_suffix = &gtk::Button {
100+
set_icon_name: "dice3-symbolic",
101+
set_tooltip_text: Some("Generate name"),
102+
set_valign: gtk::Align::Center,
103+
add_css_class: "flat",
104+
connect_clicked[topic_entry] => move |_| {
105+
use rand::distributions::Alphanumeric;
106+
use rand::{thread_rng, Rng};
107+
let mut rng = thread_rng();
108+
let chars: String = (0..10).map(|_| rng.sample(Alphanumeric) as char).collect();
109+
topic_entry.set_text(&chars);
110+
}
111+
}
112+
},
113+
append = &adw::ExpanderRow {
114+
set_title: "Custom server...",
115+
set_enable_expansion: false,
116+
set_show_enable_switch: true,
117+
add_row: server_entry = &adw::EntryRow {
118+
set_title: "Server",
119+
}
120+
}
121+
},
122+
append = &gtk::Button {
123+
set_label: "Subscribe",
124+
add_css_class: "suggested-action",
125+
add_css_class: "pill",
126+
set_halign: gtk::Align::Center,
127+
connect_clicked[obj] => move |_| {
128+
obj.emit_subscribe_request();
129+
obj.close();
130+
}
131+
}
132+
},
133+
},
134+
},
135+
}
171136

172-
content.append(&sub_btn);
173-
toolbar_view.set_content(Some(&clamp));
137+
imp.topic_entry.replace(topic_entry);
138+
imp.server_entry.replace(server_entry);
174139

175140
obj.set_content(Some(&toolbar_view));
176141
}
142+
pub fn topic(&self) -> String {
143+
self.imp().topic_entry.borrow().text().to_string()
144+
}
145+
pub fn server(&self) -> String {
146+
self.imp().server_entry.borrow().text().to_string()
147+
}
177148
fn emit_subscribe_request(&self) {
178149
self.emit_by_name::<()>("subscribe-request", &[]);
179150
}

0 commit comments

Comments
 (0)