pika-installer-gtk4/src2/drive_mount_row/imp.rs

161 lines
4.9 KiB
Rust
Raw Normal View History

use std::{cell::RefCell, env, rc::Rc, sync::OnceLock};
2024-02-11 19:02:16 +01:00
2024-02-16 22:21:09 +01:00
use adw::{prelude::*, subclass::prelude::*, *};
use glib::{clone, subclass::Signal, Properties};
2024-02-17 14:52:07 +01:00
use gtk::{glib, Orientation::Horizontal};
2024-02-11 19:02:16 +01:00
2024-02-18 18:28:14 +01:00
2024-02-11 19:02:16 +01:00
// ANCHOR: custom_button
// Object holding the state
#[derive(Properties, Default)]
#[properties(wrapper_type = super::DriveMountRow)]
pub struct DriveMountRow {
#[property(get, set)]
mountopt: RefCell<String>,
#[property(get, set)]
partition: RefCell<String>,
#[property(get, set)]
mountpoint: RefCell<String>,
#[property(get, set)]
2024-02-16 08:51:28 +01:00
partitionscroll: Rc<RefCell<gtk::ScrolledWindow>>,
2024-02-11 19:02:16 +01:00
}
// ANCHOR_END: custom_button
// The central trait for subclassing a GObject
#[glib::object_subclass]
impl ObjectSubclass for DriveMountRow {
const NAME: &'static str = "DriveMountRow";
type Type = super::DriveMountRow;
type ParentType = adw::ActionRow;
}
// ANCHOR: object_impl
// Trait shared by all GObjects
#[glib::derived_properties]
impl ObjectImpl for DriveMountRow {
fn signals() -> &'static [Signal] {
static SIGNALS: OnceLock<Vec<Signal>> = OnceLock::new();
2024-02-16 22:21:09 +01:00
SIGNALS.get_or_init(|| vec![Signal::builder("row-deleted").build()])
2024-02-11 19:02:16 +01:00
}
fn constructed(&self) {
let current_locale = match env::var_os("LANG") {
Some(v) => v.into_string().unwrap(),
None => panic!("$LANG is not set"),
};
rust_i18n::set_locale(current_locale.strip_suffix(".UTF-8").unwrap());
2024-02-11 19:02:16 +01:00
self.parent_constructed();
// Bind label to number
// `SYNC_CREATE` ensures that the label will be immediately set
let obj = self.obj();
let action_row_content_box = gtk::Box::builder()
.orientation(Horizontal)
.spacing(0)
2024-02-17 12:47:32 +01:00
.vexpand(true)
.hexpand(true)
2024-02-11 19:02:16 +01:00
.build();
let partition_row_expander_adw_listbox = gtk::ListBox::builder()
.margin_end(5)
2024-02-16 08:51:28 +01:00
.margin_start(10)
2024-02-11 19:02:16 +01:00
.margin_top(5)
.margin_bottom(5)
2024-02-17 12:47:32 +01:00
.vexpand(true)
.hexpand(true)
2024-02-11 19:02:16 +01:00
.build();
partition_row_expander_adw_listbox.add_css_class("boxed-list");
let partition_row_expander = adw::ExpanderRow::builder()
.subtitle(t!("subtitle_partition"))
2024-02-17 12:47:32 +01:00
.vexpand(true)
.hexpand(true)
2024-02-16 08:51:28 +01:00
.width_request(300)
2024-02-11 19:02:16 +01:00
.build();
2024-02-25 19:38:11 +01:00
let mountpoint_entry_row = gtk::Entry::builder()
.placeholder_text(t!("title_mountpoint"))
.hexpand(true)
2024-02-25 19:38:11 +01:00
.vexpand(true)
2024-02-11 19:02:16 +01:00
.margin_bottom(5)
2024-02-25 19:38:11 +01:00
.margin_top(5)
.width_request(300)
.build();
2024-02-25 19:38:11 +01:00
let mountopt_entry_row = gtk::Entry::builder()
.placeholder_text(t!("title_mountopt"))
.hexpand(true)
2024-02-25 19:38:11 +01:00
.vexpand(true)
.margin_start(10)
2024-02-11 19:02:16 +01:00
.margin_bottom(5)
2024-02-25 19:38:11 +01:00
.margin_top(5)
.width_request(300)
2024-02-11 19:02:16 +01:00
.build();
let partition_row_delete_button = gtk::Button::builder()
2024-02-25 19:38:11 +01:00
.margin_end(5)
2024-02-11 19:02:16 +01:00
.margin_top(5)
.margin_bottom(5)
.width_request(53)
.height_request(53)
.valign(gtk::Align::Start)
.icon_name("user-trash")
2024-02-11 19:02:16 +01:00
.halign(gtk::Align::End)
.build();
partition_row_delete_button.connect_clicked(clone!( @weak obj => move |_| {
obj.emit_by_name::<()>("row-deleted", &[]);
}));
partition_row_expander_adw_listbox.append(&partition_row_expander);
action_row_content_box.append(&partition_row_expander_adw_listbox);
2024-02-25 19:38:11 +01:00
action_row_content_box.append(&mountpoint_entry_row);
2024-02-11 19:02:16 +01:00
2024-02-25 19:38:11 +01:00
action_row_content_box.append(&mountopt_entry_row);
2024-02-11 19:02:16 +01:00
obj.add_prefix(&action_row_content_box);
2024-02-25 19:38:11 +01:00
obj.add_suffix(&partition_row_delete_button);
2024-02-11 19:02:16 +01:00
// Bind label to number
// `SYNC_CREATE` ensures that the label will be immediately set
let obj = self.obj();
obj.bind_property("partition", &partition_row_expander, "title")
.sync_create()
.bidirectional()
.build();
obj.bind_property("mountpoint", &mountpoint_entry_row, "text")
.sync_create()
.bidirectional()
.build();
obj.bind_property("mountopt", &mountopt_entry_row, "text")
.sync_create()
.bidirectional()
.build();
2024-02-16 08:51:28 +01:00
obj.connect_partitionscroll_notify(clone!(@weak obj => move |_| {
partition_row_expander.add_row(&obj.property::<gtk::ScrolledWindow>("partitionscroll"));
}));
2024-02-11 19:02:16 +01:00
}
}
// Trait shared by all widgets
impl WidgetImpl for DriveMountRow {}
// Trait shared by all buttons
// Trait shared by all buttons
impl ListBoxRowImpl for DriveMountRow {}
impl PreferencesRowImpl for DriveMountRow {}
impl ActionRowImpl for DriveMountRow {
//fn clicked(&self) {
// let incremented_number = self.obj().number() + 1;
// self.obj().set_number(incremented_number);
//}
2024-02-16 22:21:09 +01:00
}