Skip to content

Commit

Permalink
fix: Reset activated state for first click
Browse files Browse the repository at this point in the history
  • Loading branch information
DashieTM committed Dec 18, 2023
1 parent 27e003e commit f4a8693
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 50 deletions.
130 changes: 82 additions & 48 deletions src/components/window/reset_window.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::rc::Rc;

use adw::glib::clone;
use adw::subclass::prelude::ObjectSubclassIsExt;
use adw::BreakpointCondition;
Expand All @@ -23,7 +25,7 @@ unsafe impl Send for ReSetWindow {}
unsafe impl Sync for ReSetWindow {}

impl ReSetWindow {
pub fn new(app: &Application) -> Self {
pub fn new(app: &Application) -> Rc<Self> {
app.set_accels_for_action("win.search", &["<Ctrl>F"]);
app.set_accels_for_action("win.close", &["<Ctrl>Q"]);
app.set_accels_for_action("win.about", &["<Ctrl>H"]);
Expand All @@ -32,7 +34,9 @@ impl ReSetWindow {
// app.set_accels_for_action("win.right", &["<Ctrl>L"]);
// app.set_accels_for_action("win.down", &["<Ctrl>J"]);
// app.set_accels_for_action("win.left", &["<Ctrl>H"]);
Object::builder().property("application", app).build()
let mut window: Rc<Self> = Rc::new(Object::builder().property("application", app).build());
window = setup_callback(window);
window
}

pub fn setup_shortcuts(&self) {
Expand Down Expand Up @@ -76,6 +80,19 @@ impl ReSetWindow {
})
.build();

// let clear_initial = ActionEntry::builder("clear_initial")
// .activate(move |window: &Self, _, _| {
// let imp = window.imp();
// for (_, subentries) in imp.sidebar_entries.borrow().iter() {
// for subentry in subentries {
// if &*subentry.imp().name.borrow() == "Output" {
// subentry.set_state_flags(StateFlags::SELECTED, false);
// }
// }
// }
// })
// .build();

let about_action = ActionEntry::builder("about")
.activate(move |window: &ReSetWindow, _, _| {
let dialog = adw::AboutWindow::builder()
Expand Down Expand Up @@ -106,39 +123,10 @@ impl ReSetWindow {
vim_right,
vim_down,
vim_left,
// clear_initial,
]);
}

pub fn setup_callback(&self) {
let self_imp = self.imp();

self_imp.reset_search_entry.connect_search_changed(
clone!(@ weak self as window => move |_| {
window.filter_list();
}),
);

self_imp
.reset_sidebar_toggle
.connect_clicked(clone!(@ weak self as window => move |_| {
window.toggle_sidebar();
}));

self_imp.reset_sidebar_list.connect_row_activated(
clone!(@ weak self_imp as flowbox => move |_, y| {
let result = y.downcast_ref::<SidebarEntry>().unwrap();
let click_event = result.imp().on_click_event.borrow().on_click_event;
(click_event)(flowbox.listeners.clone(), flowbox.reset_main.get(), flowbox.position.clone());
}),
);

self_imp
.reset_close
.connect_clicked(clone!(@ weak self as window => move |_| {
window.close();
}));
}

pub fn handle_dynamic_sidebar(&self) {
let self_imp = self.imp();
self_imp
Expand Down Expand Up @@ -207,20 +195,20 @@ impl ReSetWindow {
let mut sidebar_entries = self_imp.sidebar_entries.borrow_mut();

let connectivity_list = vec![
SidebarEntry::new(
Rc::new(SidebarEntry::new(
"WiFi",
"network-wireless-symbolic",
Categories::Connectivity,
true,
HANDLE_WIFI_CLICK,
),
SidebarEntry::new(
)),
Rc::new(SidebarEntry::new(
"Bluetooth",
"bluetooth-symbolic",
Categories::Connectivity,
true,
HANDLE_BLUETOOTH_CLICK,
),
)),
// uncommented when VPN is implemented
// SidebarEntry::new(
// "VPN",
Expand All @@ -232,43 +220,43 @@ impl ReSetWindow {
];

sidebar_entries.push((
SidebarEntry::new(
Rc::new(SidebarEntry::new(
"Connectivity",
"network-wired-symbolic",
Categories::Connectivity,
false,
HANDLE_CONNECTIVITY_CLICK,
),
)),
connectivity_list,
));

let output = SidebarEntry::new(
let output = Rc::new(SidebarEntry::new(
"Output",
"audio-volume-high-symbolic",
Categories::Audio,
true,
HANDLE_VOLUME_CLICK,
);
));
output.set_receives_default(true);
let audio_list = vec![
output,
SidebarEntry::new(
Rc::new(SidebarEntry::new(
"Input",
"audio-input-microphone-symbolic",
Categories::Audio,
true,
HANDLE_MICROPHONE_CLICK,
),
)),
];

sidebar_entries.push((
SidebarEntry::new(
Rc::new(SidebarEntry::new(
"Audio",
"audio-headset-symbolic",
Categories::Audio,
false,
HANDLE_AUDIO_CLICK,
),
)),
audio_list,
));

Expand Down Expand Up @@ -320,16 +308,17 @@ impl ReSetWindow {
}));

for (main_entry, sub_entries) in sidebar_entries.iter() {
self_imp.reset_sidebar_list.append(main_entry);
self_imp.reset_sidebar_list.append(&**main_entry);
for sub_entry in sub_entries {
// TODO change this to home when home offers dynamic selection
// this is just a placeholder for now, hence hardcoded
if &*sub_entry.imp().name.borrow() == "Output" {
self_imp.reset_sidebar_list.append(sub_entry);
self_imp.reset_sidebar_list.append(&**sub_entry);
self_imp.default_entry.replace(Some(sub_entry.clone()));
sub_entry.grab_focus();
sub_entry.set_state_flags(StateFlags::SELECTED, true);
sub_entry.set_state_flags(StateFlags::SELECTED, false);
} else {
self_imp.reset_sidebar_list.append(sub_entry);
self_imp.reset_sidebar_list.append(&**sub_entry);
}
}
let separator = gtk::Separator::builder()
Expand All @@ -352,3 +341,48 @@ impl ReSetWindow {
}
}
}
fn setup_callback(window: Rc<ReSetWindow>) -> Rc<ReSetWindow> {
let self_imp = window.imp();
let activated_ref = window.clone();
let search_ref = window.clone();
let toggle_ref = window.clone();
let close_ref = window.clone();

self_imp
.reset_search_entry
.connect_search_changed(move |_| {
search_ref.filter_list();
});

self_imp.reset_sidebar_toggle.connect_clicked(move |_| {
toggle_ref.toggle_sidebar();
});

self_imp
.reset_sidebar_list
.connect_row_activated(move |_, y| {
let imp = activated_ref.imp();
let result = y.downcast_ref::<SidebarEntry>().unwrap();
{
let mut default_entry = imp.default_entry.borrow_mut();
if default_entry.is_some() {
default_entry
.clone()
.unwrap()
.set_state_flags(StateFlags::NORMAL, true);
*default_entry = None;
}
}
let click_event = result.imp().on_click_event.borrow().on_click_event;
(click_event)(
imp.listeners.clone(),
imp.reset_main.get(),
imp.position.clone(),
);
});

self_imp.reset_close.connect_clicked(move |_| {
close_ref.close();
});
window
}
6 changes: 4 additions & 2 deletions src/components/window/reset_window_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use crate::components::wifi::wifi_box::WifiBox;
use crate::components::window::reset_window;
use crate::components::window::sidebar_entry::SidebarEntry;

type SidebarEntries = RefCell<Vec<(Rc<SidebarEntry>, Vec<Rc<SidebarEntry>>)>>;

#[derive(CompositeTemplate, Default)]
#[template(resource = "/org/Xetibo/ReSet/resetMainWindow.ui")]
pub struct ReSetWindow {
Expand All @@ -32,7 +34,8 @@ pub struct ReSetWindow {
pub reset_sidebar_toggle: TemplateChild<Button>,
#[template_child]
pub reset_close: TemplateChild<Button>,
pub sidebar_entries: RefCell<Vec<(SidebarEntry, Vec<SidebarEntry>)>>,
pub sidebar_entries: SidebarEntries,
pub default_entry: RefCell<Option<Rc<SidebarEntry>>>,
pub listeners: Arc<Listeners>,
pub position: Rc<RefCell<Position>>,
}
Expand Down Expand Up @@ -63,7 +66,6 @@ impl ObjectImpl for ReSetWindow {

let obj = self.obj();
obj.setup_shortcuts();
obj.setup_callback();
obj.handle_dynamic_sidebar();
obj.setup_sidebar_entries();
}
Expand Down

0 comments on commit f4a8693

Please sign in to comment.