Restructure Reset

This commit is contained in:
takotori 2023-10-28 17:55:25 +02:00
parent 721e7d3f4e
commit b3f1bfd59b
16 changed files with 54 additions and 30 deletions

View file

@ -0,0 +1,43 @@
use gtk::{Button, CompositeTemplate, DropDown, TemplateChild, glib};
use gtk::prelude::*;
use gtk::subclass::prelude::*;
use crate::components::audio::AudioSourceEntry;
#[allow(non_snake_case)]
#[derive(Default, CompositeTemplate)]
#[template(resource = "/org/xetibo/reset/resetAudio.ui")]
pub struct AudioBox {
#[template_child]
pub resetOutputDevice: TemplateChild<DropDown>,
#[template_child]
pub resetAllOutputDevices: TemplateChild<Button>,
}
#[glib::object_subclass]
impl ObjectSubclass for AudioBox {
const NAME: &'static str = "resetAudio";
type Type = super::AudioBox;
type ParentType = gtk::Box;
fn class_init(klass: &mut Self::Class) {
AudioSourceEntry::ensure_type();
klass.bind_template();
}
fn instance_init(obj: &glib::subclass::InitializingObject<Self>) {
obj.init_template();
}
}
impl BoxImpl for AudioBox {}
impl ObjectImpl for AudioBox {}
impl ListBoxRowImpl for AudioBox {}
impl WidgetImpl for AudioBox {}
impl WindowImpl for AudioBox {}
impl ApplicationWindowImpl for AudioBox {}

View file

@ -0,0 +1,41 @@
use gtk::{Button, CompositeTemplate, glib, Image, Label, ProgressBar, Scale};
use gtk::subclass::prelude::*;
#[allow(non_snake_case)]
#[derive(Default, CompositeTemplate)]
#[template(resource = "/org/xetibo/reset/resetAudioSourceEntry.ui")]
pub struct AudioSourceEntry {
#[template_child]
pub resetSourceIcon: TemplateChild<Image>,
#[template_child]
pub resetSourceName: TemplateChild<Label>,
#[template_child]
pub resetSourceMute: TemplateChild<Button>,
#[template_child]
pub resetVolumeSlider: TemplateChild<Scale>,
#[template_child]
pub resetVolumePercentage: TemplateChild<Label>,
#[template_child]
pub resetVolumeMeter: TemplateChild<ProgressBar>,
}
#[glib::object_subclass]
impl ObjectSubclass for AudioSourceEntry {
const NAME: &'static str = "resetAudioSourceEntry";
type Type = super::AudioSourceEntry;
type ParentType = gtk::Box;
fn class_init(klass: &mut Self::Class) {
klass.bind_template();
}
fn instance_init(obj: &glib::subclass::InitializingObject<Self>) {
obj.init_template();
}
}
impl BoxImpl for AudioSourceEntry {}
impl ObjectImpl for AudioSourceEntry {}
impl WidgetImpl for AudioSourceEntry {}

View file

@ -0,0 +1,31 @@
#![allow(non_snake_case)]
mod audioSource;
mod audioBox;
use adw::glib::Object;
use gtk::{glib};
glib::wrapper! {
pub struct AudioBox(ObjectSubclass<audioBox::AudioBox>)
@extends gtk::Box, gtk::Widget,
@implements gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget, gtk::Orientable;
}
glib::wrapper! {
pub struct AudioSourceEntry(ObjectSubclass<audioSource::AudioSourceEntry>)
@extends gtk::Box, gtk::Widget,
@implements gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget, gtk::Orientable;
}
impl AudioBox {
pub fn new() -> Self {
Object::builder().build()
}
}
impl AudioSourceEntry {
pub fn new() -> Self {
Object::builder().build()
}
}

View file

@ -0,0 +1,46 @@
use gtk::{CompositeTemplate, glib, ListBox, Switch};
use gtk::prelude::*;
use gtk::subclass::prelude::*;
use crate::components::bluetooth::BluetoothEntry;
#[allow(non_snake_case)]
#[derive(Default, CompositeTemplate)]
#[template(resource = "/org/xetibo/reset/resetBluetooth.ui")]
pub struct BluetoothBox {
#[template_child]
pub resetBluetoothSwitch: TemplateChild<Switch>,
#[template_child]
pub resetBluetoothAvailableDevices: TemplateChild<ListBox>,
#[template_child]
pub resetBluetoothConnectedDevices: TemplateChild<ListBox>,
}
#[glib::object_subclass]
impl ObjectSubclass for BluetoothBox {
const NAME: &'static str = "resetBluetooth";
type Type = super::BluetoothBox;
type ParentType = gtk::Box;
fn class_init(klass: &mut Self::Class) {
BluetoothEntry::ensure_type();
klass.bind_template();
}
fn instance_init(obj: &glib::subclass::InitializingObject<Self>) {
obj.init_template();
}
}
impl ObjectImpl for BluetoothBox {
fn constructed(&self) {
self.parent_constructed();
}
}
impl BoxImpl for BluetoothBox {}
impl WidgetImpl for BluetoothBox {}
impl WindowImpl for BluetoothBox {}
impl ApplicationWindowImpl for BluetoothBox {}

View file

@ -0,0 +1,43 @@
use gtk::{Button, CompositeTemplate, glib, Image, Label};
use gtk::subclass::prelude::*;
#[allow(non_snake_case)]
#[derive(Default, CompositeTemplate)]
#[template(resource = "/org/xetibo/reset/resetBluetoothEntry.ui")]
pub struct BluetoothEntry {
#[template_child]
pub resetBluetoothDeviceType: TemplateChild<Image>,
#[template_child]
pub resetBluetoothLabel: TemplateChild<Label>,
#[template_child]
pub resetBluetoothButton: TemplateChild<Button>,
}
#[glib::object_subclass]
impl ObjectSubclass for BluetoothEntry {
const NAME: &'static str = "resetBluetoothEntry";
type Type = super::BluetoothEntry;
type ParentType = gtk::ListBoxRow;
fn class_init(klass: &mut Self::Class) {
klass.bind_template();
}
fn instance_init(obj: &glib::subclass::InitializingObject<Self>) {
obj.init_template();
}
}
impl ObjectImpl for BluetoothEntry {
fn constructed(&self) {
self.parent_constructed();
}
}
impl ListBoxRowImpl for BluetoothEntry {}
impl WidgetImpl for BluetoothEntry {}
impl WindowImpl for BluetoothEntry {}
impl ApplicationWindowImpl for BluetoothEntry {}

View file

@ -0,0 +1,30 @@
#![allow(non_snake_case)]
mod bluetoothBox;
mod bluetoothEntry;
use adw::glib::Object;
use gtk::{glib};
glib::wrapper! {
pub struct BluetoothBox(ObjectSubclass<bluetoothBox::BluetoothBox>)
@extends gtk::Box, gtk::Widget,
@implements gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget, gtk::Orientable;
}
glib::wrapper! {
pub struct BluetoothEntry(ObjectSubclass<bluetoothEntry::BluetoothEntry>)
@extends gtk::Widget,
@implements gtk::Accessible, gtk::Buildable, gtk::Actionable, gtk::ConstraintTarget;
}
impl BluetoothBox {
pub fn new() -> Self {
Object::builder().build()
}
}
impl BluetoothEntry {
pub fn new() -> Self {
Object::builder().build()
}
}

4
src/components/mod.rs Normal file
View file

@ -0,0 +1,4 @@
pub mod window;
pub mod wifi;
pub mod bluetooth;
pub mod audio;

View file

@ -0,0 +1,48 @@
#![allow(non_snake_case)]
use std::thread;
use std::time::Duration;
use adw::glib::Object;
use dbus::blocking::Connection;
use dbus::Error;
use gtk::glib;
mod wifiBox;
mod wifiEntry;
glib::wrapper! {
pub struct WifiBox(ObjectSubclass<wifiBox::WifiBox>)
@extends gtk::Box, gtk::Widget,
@implements gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget, gtk::Orientable;
}
glib::wrapper! {
pub struct WifiEntry(ObjectSubclass<wifiEntry::WifiEntry>)
@extends gtk::Widget,
@implements gtk::Accessible, gtk::Buildable, gtk::Actionable, gtk::ConstraintTarget;
}
impl WifiBox {
pub fn new() -> Self {
Object::builder().build()
}
pub fn donotdisturb() {
thread::spawn(|| {
let conn = Connection::new_session().unwrap();
let proxy = conn.with_proxy(
"org.freedesktop.Notifications",
"/org/freedesktop/Notifications",
Duration::from_millis(1000),
);
let _ : Result<(), Error> = proxy.method_call("org.freedesktop.Notifications", "DoNotDisturb", ());
});
}
}
impl WifiEntry {
pub fn new() -> Self {
Object::builder().build()
}
}

View file

@ -0,0 +1,43 @@
use gtk::{CompositeTemplate, glib, ListBox};
use gtk::prelude::*;
use gtk::subclass::prelude::*;
use crate::components::wifi::WifiEntry;
#[allow(non_snake_case)]
#[derive(Default, CompositeTemplate)]
#[template(resource = "/org/xetibo/reset/resetWiFi.ui")]
pub struct WifiBox {
#[template_child]
pub resetWifiList: TemplateChild<ListBox>,
}
#[glib::object_subclass]
impl ObjectSubclass for WifiBox {
const NAME: &'static str = "resetWifi";
type Type = super::WifiBox;
type ParentType = gtk::Box;
fn class_init(klass: &mut Self::Class) {
WifiEntry::ensure_type();
klass.bind_template();
}
fn instance_init(obj: &glib::subclass::InitializingObject<Self>) {
obj.init_template();
}
}
impl ObjectImpl for WifiBox {
fn constructed(&self) {
self.parent_constructed();
}
}
impl BoxImpl for WifiBox {}
impl WidgetImpl for WifiBox {}
impl WindowImpl for WifiBox {}
impl ApplicationWindowImpl for WifiBox {}

View file

@ -0,0 +1,43 @@
use gtk::{Button, CompositeTemplate, glib, Image, Label};
use gtk::subclass::prelude::*;
#[allow(non_snake_case)]
#[derive(Default, CompositeTemplate)]
#[template(resource = "/org/xetibo/reset/resetWifiEntry.ui")]
pub struct WifiEntry {
#[template_child]
pub resetWifiStrength: TemplateChild<Image>,
#[template_child]
pub resetWifiLabel: TemplateChild<Label>,
#[template_child]
pub resetWifiButton: TemplateChild<Button>,
}
#[glib::object_subclass]
impl ObjectSubclass for WifiEntry {
const NAME: &'static str = "resetWifiEntry";
type Type = super::WifiEntry;
type ParentType = gtk::ListBoxRow;
fn class_init(klass: &mut Self::Class) {
klass.bind_template();
}
fn instance_init(obj: &glib::subclass::InitializingObject<Self>) {
obj.init_template();
}
}
impl ObjectImpl for WifiEntry {
fn constructed(&self) {
self.parent_constructed();
}
}
impl ListBoxRowImpl for WifiEntry {}
impl WidgetImpl for WifiEntry {}
impl WindowImpl for WifiEntry {}
impl ApplicationWindowImpl for WifiEntry {}

View file

@ -0,0 +1,50 @@
use gtk::FlowBox;
use crate::components::audio::AudioBox;
use crate::components::bluetooth::BluetoothBox;
use crate::components::wifi::WifiBox;
pub const HANDLE_CONNECTIVITY_CLICK: fn(FlowBox) = |resetMain: FlowBox| {
let wifibox = WifiBox::new();
resetMain.remove_all();
resetMain.insert(&wifibox, -1);
};
pub const HANDLE_WIFI_CLICK: fn(FlowBox) = |resetMain: FlowBox| {
let wifibox = WifiBox::new();
resetMain.remove_all();
resetMain.insert(&wifibox, -1);
};
pub const HANDLE_BLUETOOTH_CLICK: fn(FlowBox) = |resetMain: FlowBox| {
let bluetoothBox = BluetoothBox::new();
resetMain.remove_all();
resetMain.insert(&bluetoothBox, -1);
};
pub const HANDLE_VPN_CLICK: fn(FlowBox) = |resetMain: FlowBox| {
let wifibox = WifiBox::new();
resetMain.remove_all();
resetMain.insert(&wifibox, -1);
};
pub const HANDLE_AUDIO_CLICK: fn(FlowBox) = |resetMain: FlowBox| {
let audioBox = AudioBox::new();
resetMain.remove_all();
resetMain.insert(&audioBox, -1);
};
pub const HANDLE_VOLUME_CLICK: fn(FlowBox) = |resetMain: FlowBox| {
let audioBox = AudioBox::new();
resetMain.remove_all();
resetMain.insert(&audioBox, -1);
};
pub const HANDLE_MICROPHONE_CLICK: fn(FlowBox) = |resetMain: FlowBox| {
let wifibox = WifiBox::new();
resetMain.remove_all();
resetMain.insert(&wifibox, -1);
};
pub const HANDLE_HOME: fn(FlowBox) = |resetMain: FlowBox| {
resetMain.remove_all();
};

View file

@ -0,0 +1,244 @@
#![allow(non_snake_case)]
use adw::BreakpointCondition;
use adw::glib::clone;
use adw::subclass::prelude::ObjectSubclassIsExt;
use glib::Object;
use gtk::{Application, FlowBox, gio, glib};
use gtk::prelude::*;
use crate::components::wifi::WifiBox;
use crate::components::window::handleSidebarClick::{
HANDLE_AUDIO_CLICK, HANDLE_BLUETOOTH_CLICK, HANDLE_CONNECTIVITY_CLICK, HANDLE_MICROPHONE_CLICK,
HANDLE_VOLUME_CLICK, HANDLE_VPN_CLICK, HANDLE_WIFI_CLICK,
};
use crate::components::window::sidebarEntry::{Categories, SidebarAction};
mod handleSidebarClick;
mod sidebarEntry;
mod window;
glib::wrapper! {
pub struct Window(ObjectSubclass<window::Window>)
@extends gtk::ApplicationWindow, gtk::Window, gtk::Widget,
@implements gio::ActionGroup, gio::ActionMap, gtk::Accessible, gtk::Buildable,
gtk::ConstraintTarget, gtk::Native, gtk::Root, gtk::ShortcutManager;
}
glib::wrapper! {
pub struct SidebarEntry(ObjectSubclass<sidebarEntry::SidebarEntry>)
@extends gtk::ListBoxRow, gtk::Widget,
@implements gtk::Accessible, gtk::Actionable, gtk::Buildable, gtk::ConstraintTarget;
}
#[allow(non_snake_case)]
impl Window {
pub fn new(app: &Application) -> Self {
Object::builder().property("application", app).build()
}
fn setupCallback(&self) {
let selfImp = self.imp();
selfImp.resetSearchEntry
.connect_search_changed(clone!(@ weak self as window => move |_| {
window.filterList();
}));
selfImp.resetSideBarToggle
.connect_clicked(clone!(@ weak self as window => move |_| {
window.toggleSidebar();
}));
selfImp.resetSidebarList.connect_row_activated(
clone!(@ weak selfImp as flowbox => move |_, y| {
let result = y.downcast_ref::<SidebarEntry>().unwrap();
let clickEvent = result.imp().onClickEvent.borrow().onClickEvent;
(clickEvent)(flowbox.resetMain.get());
}),
);
selfImp.resetClose.connect_clicked(clone!(@ weak self as window => move |_| {
window.close();
}));
selfImp.resetMenu.connect_clicked(|_| {
WifiBox::donotdisturb();
});
}
fn handleDynamicSidebar(&self) {
let selfImp = self.imp();
selfImp
.resetSidebarBreakpoint
.set_condition(BreakpointCondition::parse("max-width: 500sp").as_ref().ok());
selfImp.resetSidebarBreakpoint.add_setter(
&Object::from(selfImp.resetOverlaySplitView.get()),
"collapsed",
&true.to_value(),
);
selfImp.resetSidebarBreakpoint.add_setter(
&Object::from(selfImp.resetSideBarToggle.get()),
"visible",
&true.to_value(),
);
}
fn filterList(&self) {
let text = self.imp().resetSearchEntry.text().to_string();
for (mainEntry, subEntries) in self.imp().sidebarEntries.borrow().iter() {
if text == "" {
mainEntry.set_visible(true);
for subEntry in subEntries {
subEntry.set_visible(true);
}
continue;
}
if mainEntry
.imp()
.name
.borrow()
.to_lowercase()
.contains(&text.to_lowercase())
{
mainEntry.set_visible(true);
} else {
mainEntry.set_visible(false);
}
for subEntry in subEntries {
if subEntry
.imp()
.name
.borrow()
.to_lowercase()
.contains(&text.to_lowercase())
{
subEntry.set_visible(true);
mainEntry.set_visible(true);
} else {
subEntry.set_visible(false);
}
}
}
}
fn toggleSidebar(&self) {
if self.imp().resetOverlaySplitView.shows_sidebar() {
self.imp().resetOverlaySplitView.set_show_sidebar(false);
} else {
self.imp().resetOverlaySplitView.set_show_sidebar(true);
}
}
fn setupSidebarEntries(&self) {
let selfImp = self.imp();
let mut sidebarEntries = selfImp.sidebarEntries.borrow_mut();
let connectivityList = vec![
SidebarEntry::new(
"WiFi",
"network-wireless-symbolic",
Categories::Connectivity,
true,
HANDLE_WIFI_CLICK,
),
SidebarEntry::new(
"Bluetooth",
"bluetooth-symbolic",
Categories::Connectivity,
true,
HANDLE_BLUETOOTH_CLICK,
),
SidebarEntry::new(
"VPN",
"network-vpn-symbolic",
Categories::Connectivity,
true,
HANDLE_VPN_CLICK,
),
];
sidebarEntries.push((
SidebarEntry::new(
"Connectivity",
"network-wired-symbolic",
Categories::Connectivity,
false,
HANDLE_CONNECTIVITY_CLICK,
),
connectivityList,
));
let audioList = vec![
SidebarEntry::new(
"Volume",
"audio-volume-high-symbolic",
Categories::Audio,
true,
HANDLE_VOLUME_CLICK,
),
SidebarEntry::new(
"Microphone",
"audio-input-microphone-symbolic",
Categories::Audio,
true,
HANDLE_MICROPHONE_CLICK,
),
];
sidebarEntries.push((
SidebarEntry::new(
"Audio",
"audio-headset-symbolic",
Categories::Audio,
false,
HANDLE_AUDIO_CLICK,
),
audioList,
));
for (mainEntry, subEntries) in sidebarEntries.iter() {
selfImp.resetSidebarList.append(mainEntry);
for subEntry in subEntries {
selfImp.resetSidebarList.append(subEntry);
}
}
}
}
impl SidebarEntry {
pub fn new(
entryName: &str,
iconName: &str,
category: Categories,
isSubcategory: bool,
clickEvent: fn(FlowBox),
) -> Self {
let entry: SidebarEntry = Object::builder().build();
let entryImp = entry.imp();
entryImp.resetSidebarLabel.get().set_text(entryName);
entryImp
.resetSidebarImage
.set_from_icon_name(Some(iconName));
entryImp.category.set(category);
entryImp.isSubcategory.set(isSubcategory);
{
let mut name = entryImp.name.borrow_mut();
*name = String::from(entryName);
let mut action = entryImp.onClickEvent.borrow_mut();
*action = SidebarAction {
onClickEvent: clickEvent,
};
}
Self::setMargin(&entry);
entry
}
fn setMargin(entry: &SidebarEntry) {
if entry.imp().isSubcategory.get() {
let option = entry.child().unwrap();
option.set_margin_start(30);
}
}
}

View file

@ -0,0 +1,63 @@
use std::cell::{Cell, RefCell};
use glib::subclass::InitializingObject;
use gtk::{CompositeTemplate, FlowBox, glib, Image, Label, ListBoxRow};
use gtk::subclass::prelude::*;
use crate::components::window::handleSidebarClick::HANDLE_HOME;
#[derive(Default)]
pub enum Categories {
Connectivity,
Audio,
#[default]
Misc,
}
#[allow(non_snake_case)]
#[derive(CompositeTemplate, Default)]
#[template(resource = "/org/xetibo/reset/resetSidebarEntry.ui")]
pub struct SidebarEntry {
#[template_child]
pub resetSidebarLabel: TemplateChild<Label>,
#[template_child]
pub resetSidebarImage: TemplateChild<Image>,
pub category: Cell<Categories>,
pub isSubcategory: Cell<bool>,
pub onClickEvent: RefCell<SidebarAction>,
pub name : RefCell<String>,
}
#[allow(non_snake_case)]
pub struct SidebarAction {
pub onClickEvent: fn(FlowBox),
}
impl Default for SidebarAction {
fn default() -> Self {
Self {
onClickEvent: HANDLE_HOME
}
}
}
#[glib::object_subclass]
impl ObjectSubclass for SidebarEntry {
const NAME: &'static str = "resetSidebarEntry";
type Type = super::SidebarEntry;
type ParentType = ListBoxRow;
fn class_init(klass: &mut Self::Class) {
klass.bind_template();
}
fn instance_init(obj: &InitializingObject<Self>) {
obj.init_template();
}
}
impl ObjectImpl for SidebarEntry {}
impl ListBoxRowImpl for SidebarEntry {}
impl WidgetImpl for SidebarEntry {}

View file

@ -0,0 +1,70 @@
use adw::glib::StaticTypeExt;
use adw::subclass::prelude::AdwApplicationWindowImpl;
use adw::{Breakpoint, OverlaySplitView};
use glib::subclass::InitializingObject;
use gtk::subclass::prelude::*;
use gtk::{glib, Box, Button, CompositeTemplate, FlowBox, ListBox, SearchEntry};
use std::cell::RefCell;
use crate::components::wifi::WifiBox;
use crate::components::window::SidebarEntry;
#[allow(non_snake_case)]
#[derive(CompositeTemplate, Default)]
#[template(resource = "/org/xetibo/reset/resetMainWindow.ui")]
pub struct Window {
#[template_child]
pub resetMain: TemplateChild<FlowBox>,
#[template_child]
pub resetSidebarBreakpoint: TemplateChild<Breakpoint>,
#[template_child]
pub resetOverlaySplitView: TemplateChild<OverlaySplitView>,
#[template_child]
pub resetSearchEntry: TemplateChild<SearchEntry>,
#[template_child]
pub resetSidebarList: TemplateChild<ListBox>,
#[template_child]
pub resetSideBarToggle: TemplateChild<Button>,
#[template_child]
pub resetPath: TemplateChild<Box>,
#[template_child]
pub resetMenu: TemplateChild<Button>,
#[template_child]
pub resetClose: TemplateChild<Button>,
pub sidebarEntries: RefCell<Vec<(SidebarEntry, Vec<SidebarEntry>)>>,
}
#[glib::object_subclass]
impl ObjectSubclass for Window {
const NAME: &'static str = "resetUI";
type Type = super::Window;
type ParentType = adw::ApplicationWindow;
fn class_init(klass: &mut Self::Class) {
WifiBox::ensure_type();
klass.bind_template();
}
fn instance_init(obj: &InitializingObject<Self>) {
obj.init_template();
}
}
impl ObjectImpl for Window {
fn constructed(&self) {
self.parent_constructed();
let object = self.obj();
object.setupCallback();
object.handleDynamicSidebar();
object.setupSidebarEntries();
}
}
impl WidgetImpl for Window {}
impl WindowImpl for Window {}
impl ApplicationWindowImpl for Window {}
impl AdwApplicationWindowImpl for Window {}