Skip to main content

mimick/settings_window/
connectivity.rs

1use adw::prelude::*;
2use gtk::{Button, Entry, PasswordEntry, Switch};
3use libadwaita as adw;
4
5pub struct ConnectivityWidgets {
6    pub internal_switch: Switch,
7    pub external_switch: Switch,
8    pub internal_entry: Entry,
9    pub external_entry: Entry,
10    pub api_key_entry: PasswordEntry,
11    pub test_btn: Button,
12    pub save_btn: Button,
13}
14
15pub fn build_connectivity_group(
16    settings_page: &adw::PreferencesPage,
17    window: &adw::ApplicationWindow,
18) -> ConnectivityWidgets {
19    let conn_group = adw::PreferencesGroup::builder()
20        .title("Connectivity")
21        .build();
22    settings_page.add(&conn_group);
23
24    let (internal_row, internal_switch, internal_entry) =
25        add_url_row(&conn_group, "Internal URL (LAN)", "http://...");
26    let (external_row, external_switch, external_entry) =
27        add_url_row(&conn_group, "External URL (WAN)", "https://...");
28    let api_key_entry = add_api_key_row(&conn_group);
29    let test_btn = add_group_button(&conn_group, "Test Connection", None, 12);
30    let save_btn = add_group_button(&conn_group, "Save Credentials", Some("suggested-action"), 6);
31
32    add_connectivity_breakpoint(
33        window,
34        &internal_row,
35        &external_row,
36        &internal_entry,
37        &external_entry,
38        &api_key_entry,
39    );
40
41    ConnectivityWidgets {
42        internal_switch,
43        external_switch,
44        internal_entry,
45        external_entry,
46        api_key_entry,
47        test_btn,
48        save_btn,
49    }
50}
51
52fn add_url_row(
53    group: &adw::PreferencesGroup,
54    title: &str,
55    placeholder: &str,
56) -> (adw::ActionRow, Switch, Entry) {
57    let row = adw::ActionRow::builder()
58        .title(title)
59        .title_lines(1)
60        .build();
61    let switch = Switch::builder().valign(gtk::Align::Center).build();
62    let entry = text_entry(placeholder);
63    row.add_prefix(&switch);
64    row.add_suffix(&entry);
65    group.add(&row);
66    (row, switch, entry)
67}
68
69fn add_api_key_row(group: &adw::PreferencesGroup) -> PasswordEntry {
70    let row = adw::ActionRow::builder().title("API Key").build();
71    let entry = PasswordEntry::builder()
72        .valign(gtk::Align::Center)
73        .width_request(140)
74        .max_width_chars(16)
75        .hexpand(true)
76        .build();
77    row.add_suffix(&entry);
78    group.add(&row);
79    entry
80}
81
82fn text_entry(placeholder: &str) -> Entry {
83    Entry::builder()
84        .placeholder_text(placeholder)
85        .valign(gtk::Align::Center)
86        .width_request(140)
87        .max_width_chars(16)
88        .hexpand(true)
89        .build()
90}
91
92fn add_group_button(
93    group: &adw::PreferencesGroup,
94    label: &str,
95    css_class: Option<&str>,
96    margin_top: i32,
97) -> Button {
98    let mut builder = Button::builder().label(label).margin_top(margin_top);
99    if let Some(css_class) = css_class {
100        builder = builder.css_classes(vec![css_class.to_string()]);
101    }
102    let button = builder.build();
103    group.add(&button);
104    button
105}
106
107fn add_connectivity_breakpoint(
108    window: &adw::ApplicationWindow,
109    internal_row: &adw::ActionRow,
110    external_row: &adw::ActionRow,
111    internal_entry: &Entry,
112    external_entry: &Entry,
113    api_key_entry: &PasswordEntry,
114) {
115    let breakpoint = adw::Breakpoint::new(
116        adw::BreakpointCondition::parse("max-width: 500sp").expect("valid breakpoint condition"),
117    );
118    breakpoint.add_setter(internal_row, "title", Some(&"LAN URL".to_value()));
119    breakpoint.add_setter(external_row, "title", Some(&"WAN URL".to_value()));
120    breakpoint.add_setter(internal_entry, "width-request", Some(&140i32.to_value()));
121    breakpoint.add_setter(external_entry, "width-request", Some(&140i32.to_value()));
122    breakpoint.add_setter(api_key_entry, "width-request", Some(&140i32.to_value()));
123    window.add_breakpoint(breakpoint);
124}