Skip to content

Commit

Permalink
refactor: drop app_settings util func
Browse files Browse the repository at this point in the history
  • Loading branch information
SeaDve committed Jul 24, 2023
1 parent e70ea98 commit d52dbaa
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 64 deletions.
46 changes: 23 additions & 23 deletions src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,17 @@ mod imp {
fn activate(&self) {
self.parent_activate();

if let Some(window) = self.obj().main_window() {
let obj = self.obj();

if let Some(window) = self.window.get() {
let window = window.upgrade().unwrap();
window.present();
return;
}

let window = Window::new(&obj);
self.window.set(window.downgrade()).unwrap();
window.present();
}

fn startup(&self) {
Expand Down Expand Up @@ -87,18 +95,13 @@ impl Application {
})
}

pub fn main_window(&self) -> Option<Window> {
let main_window = self
.imp()
pub fn window(&self) -> Window {
self.imp()
.window
.get_or_init(|| Window::new(self).downgrade())
.upgrade();

if main_window.is_none() {
tracing::warn!("Failed to upgrade WeakRef<Window>");
}

main_window
.get()
.expect("window must be initialized on activate")
.upgrade()
.unwrap()
}

pub fn send_record_success_notification(&self, recording_file: &gio::File) {
Expand All @@ -119,9 +122,9 @@ impl Application {
}

pub fn present_preferences(&self) {
let window = PreferencesWindow::new();
let window = PreferencesWindow::new(self.settings());
window.set_modal(true);
window.set_transient_for(self.main_window().as_ref());
window.set_transient_for(Some(&self.window()));
window.present();
}

Expand All @@ -135,15 +138,13 @@ impl Application {

async fn try_show_uri(&self, uri: &str) {
if let Err(err) = gtk::FileLauncher::new(Some(&gio::File::for_uri(uri)))
.launch_future(self.main_window().as_ref())
.launch_future(Some(&self.window()))
.await
{
if !err.matches(gio::IOErrorEnum::Cancelled) {
tracing::error!("Failed to launch default for uri `{}`: {:?}", uri, err);

if let Some(window) = self.main_window() {
window.present_error(&err.into());
}
self.window().present_error(&err.into());
}
}
}
Expand All @@ -169,7 +170,7 @@ impl Application {

utils::spawn(async move {
if let Err(err) = gtk::FileLauncher::new(Some(&gio::File::for_uri(&uri)))
.open_containing_folder_future(obj.main_window().as_ref())
.open_containing_folder_future(Some(&obj.window()))
.await
{
tracing::warn!("Failed to show items: {:?}", err);
Expand All @@ -182,7 +183,7 @@ impl Application {

let action_about = gio::SimpleAction::new("about", None);
action_about.connect_activate(clone!(@weak self as obj => move |_, _| {
about::present_window(obj.main_window().as_ref());
about::present_window(Some(&obj.window()));
}));
self.add_action(&action_about);

Expand All @@ -194,13 +195,12 @@ impl Application {

let action_quit = gio::SimpleAction::new("quit", None);
action_quit.connect_activate(clone!(@weak self as obj => move |_, _| {
if let Some(window) = obj.main_window() {
if let Some(window) = obj.imp().window.get().and_then(|window| window.upgrade()) {
if let Err(err) = window.close() {
tracing::warn!("Failed to close window: {:?}", err);
}
} else {
obj.quit();
}
obj.quit();
}));
self.add_action(&action_quit);
}
Expand Down
48 changes: 25 additions & 23 deletions src/preferences_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,25 @@ use gtk::{
glib::{self, clone, closure},
pango,
};
use once_cell::unsync::OnceCell;

use crate::{
profile::{self, BoxedProfile},
settings::Settings,
utils,
};

mod imp {
use super::*;
use gtk::CompositeTemplate;

#[derive(Debug, Default, CompositeTemplate)]
#[derive(Debug, Default, glib::Properties, CompositeTemplate)]
#[properties(wrapper_type = super::PreferencesWindow)]
#[template(resource = "/io/github/seadve/Kooha/ui/preferences-window.ui")]
pub struct PreferencesWindow {
#[property(get, set, construct_only)]
pub(super) settings: OnceCell<Settings>,

#[template_child]
pub(super) framerate_button: TemplateChild<gtk::SpinButton>,
#[template_child]
Expand All @@ -40,7 +46,7 @@ mod imp {
klass.bind_template();

klass.install_action("preferences.select-saving-location", None, |obj, _, _| {
utils::app_settings().select_saving_location(obj);
obj.settings().select_saving_location(obj);
});
}

Expand All @@ -50,11 +56,13 @@ mod imp {
}

impl ObjectImpl for PreferencesWindow {
crate::derived_properties!();

fn constructed(&self) {
self.parent_constructed();

let obj = self.obj();
let settings = utils::app_settings();
let settings = obj.settings();

self.profile_row
.set_factory(Some(&profile_row_factory(&self.profile_row, false)));
Expand Down Expand Up @@ -117,12 +125,13 @@ mod imp {

// Load last active profile first in `update_profile_row` before
// connecting to the signal to avoid unnecessary updates.
self.profile_row.connect_selected_item_notify(|row| {
if let Some(item) = row.selected_item() {
let profile = item.downcast::<BoxedProfile>().unwrap();
utils::app_settings().set_profile(profile.get());
}
});
self.profile_row
.connect_selected_item_notify(clone!(@weak obj => move |row| {
if let Some(item) = row.selected_item() {
let profile = item.downcast::<BoxedProfile>().unwrap();
obj.settings().set_profile(profile.get());
}
}));
}
}

Expand All @@ -138,15 +147,14 @@ glib::wrapper! {
}

impl PreferencesWindow {
pub fn new() -> Self {
glib::Object::builder().build()
pub fn new(settings: &Settings) -> Self {
glib::Object::builder()
.property("settings", settings)
.build()
}

fn update_file_chooser_button(&self) {
let saving_location_display = utils::app_settings()
.saving_location()
.display()
.to_string();
let saving_location_display = self.settings().saving_location().display().to_string();

if let Some(stripped) =
saving_location_display.strip_prefix(&glib::home_dir().display().to_string())
Expand All @@ -162,7 +170,7 @@ impl PreferencesWindow {
}

fn update_profile_row(&self) {
let active_profile = utils::app_settings().profile();
let active_profile = self.settings().profile();

let imp = self.imp();
let position = imp
Expand Down Expand Up @@ -192,7 +200,7 @@ impl PreferencesWindow {

fn update_framerate_warning(&self) {
let imp = self.imp();
let settings = utils::app_settings();
let settings = self.settings();

imp.framerate_warning.set_visible(
settings
Expand All @@ -205,12 +213,6 @@ impl PreferencesWindow {
}
}

impl Default for PreferencesWindow {
fn default() -> Self {
Self::new()
}
}

fn profile_row_factory(
profile_row: &adw::ComboRow,
show_selected_indicator: bool,
Expand Down
3 changes: 1 addition & 2 deletions src/recording.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,7 @@ impl Recording {
// select area
if settings.capture_mode() == CaptureMode::Selection {
let data =
AreaSelector::present(utils::app_instance().main_window().as_ref(), fd, &streams)
.await?;
AreaSelector::present(Some(&utils::app_instance().window()), fd, &streams).await?;
pipeline_builder.select_area_data(data);
}

Expand Down
33 changes: 23 additions & 10 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,32 @@ use gtk::{

use std::{env, path::Path};

use crate::{settings::Settings, Application};
use crate::Application;

const MAX_THREAD_COUNT: u32 = 64;

/// Generates the boilerplate for setting `glib::Properties`'s generated functions
/// to the implementation of the following methods of `ObjectImpl`:
/// * `properties()`
/// * `property()`
/// * `set_property()`
#[macro_export]
macro_rules! derived_properties {
() => {
fn properties() -> &'static [glib::ParamSpec] {
Self::derived_properties()
}

fn property(&self, id: usize, pspec: &glib::ParamSpec) -> glib::Value {
self.derived_property(id, pspec)
}

fn set_property(&self, id: usize, value: &glib::Value, pspec: &glib::ParamSpec) {
self.derived_set_property(id, value, pspec);
}
};
}

/// Spawns a future in the default [`glib::MainContext`]
pub fn spawn<F: std::future::Future<Output = ()> + 'static>(fut: F) {
let ctx = glib::MainContext::default();
Expand All @@ -30,15 +52,6 @@ pub fn app_instance() -> Application {
gio::Application::default().unwrap().downcast().unwrap()
}

/// Get the global instance of `Settings`.
///
/// # Panics
/// Panics if the application is not running or if this is
/// called on a non-main thread.
pub fn app_settings() -> Settings {
app_instance().settings().clone()
}

/// Whether the application is running in a flatpak sandbox.
pub fn is_flatpak() -> bool {
Path::new("/.flatpak-info").exists()
Expand Down
21 changes: 15 additions & 6 deletions src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ mod imp {
});

klass.install_action("win.forget-video-sources", None, move |_obj, _, _| {
utils::app_settings().set_screencast_restore_token("");
utils::app_instance()
.settings()
.set_screencast_restore_token("");
});
}

Expand Down Expand Up @@ -239,7 +241,9 @@ impl Window {
imp.recording
.replace(Some((recording.clone(), handler_ids)));

recording.start(Some(self), &utils::app_settings()).await;
recording
.start(Some(self), utils::app_instance().settings())
.await;
}

fn toggle_pause(&self) -> Result<()> {
Expand Down Expand Up @@ -378,14 +382,15 @@ impl Window {
fn update_title_label(&self) {
let imp = self.imp();

match utils::app_settings().capture_mode() {
match utils::app_instance().settings().capture_mode() {
CaptureMode::MonitorWindow => imp.title.set_title(&gettext("Normal")),
CaptureMode::Selection => imp.title.set_title(&gettext("Selection")),
}
}

fn update_audio_toggles_sensitivity(&self) {
let is_enabled = utils::app_settings()
let is_enabled = utils::app_instance()
.settings()
.profile()
.map_or(true, |profile| profile.supports_audio());

Expand All @@ -394,7 +399,10 @@ impl Window {
}

fn update_forget_video_sources_action(&self) {
let has_restore_token = !utils::app_settings().screencast_restore_token().is_empty();
let has_restore_token = !utils::app_instance()
.settings()
.screencast_restore_token()
.is_empty();

self.imp()
.forget_video_sources_revealer
Expand All @@ -404,7 +412,8 @@ impl Window {
}

fn setup_settings(&self) {
let settings = utils::app_settings();
let app = utils::app_instance();
let settings = app.settings();

settings.connect_capture_mode_changed(clone!(@weak self as obj => move |_| {
obj.update_title_label();
Expand Down

0 comments on commit d52dbaa

Please sign in to comment.