window.rs 8.57 KB
Newer Older
1
use super::application::{Action, Application};
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
2
use super::colour;
3
use super::colour_entry::ColourEntry;
4
use super::config::{APP_ID, PROFILE};
5
6
7
8
use super::contrast_level::ContrastLevelBar;
use super::contrast_preview::ContrastPreview;
use super::window_state::WindowState;

9
use gettextrs::gettext;
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
10
use gio::prelude::*;
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
11
12
13
use glib::subclass;
use glib::subclass::prelude::*;
use glib::translate::*;
14
use glib::ObjectExt;
15
use gtk::prelude::*;
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
16
17
18
use gtk::subclass::prelude::ApplicationWindowImpl as GtkApplicationWindowImpl;
use gtk::subclass::prelude::{BinImpl, ContainerImpl, WidgetImpl, WindowImpl};
use libhandy::subclass::prelude::ApplicationWindowImpl;
19
use libhandy::HeaderBarExt;
20
use std::{cell::RefCell, rc::Rc};
Bilal Elmoussaoui's avatar
Init  
Bilal Elmoussaoui committed
21

Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
22
pub struct WindowPrivate {
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
23
24
    fg_entry: ColourEntry,
    bg_entry: ColourEntry,
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
25
26
    state: Rc<RefCell<WindowState>>,
    levelbar: RefCell<ContrastLevelBar>,
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
27
    pub preview: ContrastPreview,
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
28
    handle: libhandy::WindowHandle,
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
29
    sender: RefCell<Option<glib::Sender<Action>>>,
Bilal Elmoussaoui's avatar
Init  
Bilal Elmoussaoui committed
30
31
}

Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
32
33
impl ObjectSubclass for WindowPrivate {
    const NAME: &'static str = "Window";
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
34
    type ParentType = libhandy::ApplicationWindow;
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
35
36
37
38
    type Instance = subclass::simple::InstanceStruct<Self>;
    type Class = subclass::simple::ClassStruct<Self>;

    glib_object_subclass!();
Bilal Elmoussaoui's avatar
Init  
Bilal Elmoussaoui committed
39

Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
40
    fn new() -> Self {
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
41
42
        let bg_entry = ColourEntry::new(&gettext("Background Colour"));
        let fg_entry = ColourEntry::new(&gettext("Foreground Colour"));
43

44
        let state = Rc::new(RefCell::new(WindowState::init()));
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
45
        let handle = libhandy::WindowHandle::new();
46

Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
47
        let levelbar = RefCell::new(ContrastLevelBar::new());
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
48
        let preview = ContrastPreview::new();
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
49
        Self {
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
50
            bg_entry,
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
51
            fg_entry,
52
53
            levelbar,
            preview,
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
54
            state,
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
55
            handle,
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
            sender: RefCell::new(None),
        }
    }
}

impl ObjectImpl for WindowPrivate {
    glib_object_impl!();

    fn constructed(&self, obj: &glib::Object) {
        self.parent_constructed(obj);

        let self_ = obj.downcast_ref::<Window>().unwrap();

        if PROFILE == "Devel" {
            self_.get_style_context().add_class("devel");
        }
72
        self_.get_style_context().add_class("main-window");
73
74
        self_.set_icon_name(Some(APP_ID));
        // Keyboard Shortcuts
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
75
        let builder = gtk::Builder::from_resource("/org/gnome/design/Contrast/shortcuts.ui");
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
76
77
78
        get_widget!(builder, gtk::ShortcutsWindow, shortcuts_dialog);
        self_.set_help_overlay(Some(&shortcuts_dialog));

Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
79
        let main_container = gtk::Box::new(gtk::Orientation::Vertical, 0);
80
81
        // Header Bar
        let headerbar = libhandy::HeaderBar::new();
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
82
        headerbar.set_centering_policy(libhandy::CenteringPolicy::Strict);
83
84
        headerbar.set_show_close_button(true);
        headerbar.set_title(Some(&gettext("Contrast")));
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
85
        main_container.add(&headerbar);
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
86
87
88
89
90

        self.fg_entry.get_entry().get_style_context().add_class("fg-entry");
        self.bg_entry.get_entry().get_style_context().add_class("bg-entry");

        let reverse_btn = gtk::Button::new();
91
        reverse_btn.set_tooltip_text(Some(&gettext("Reverse Colours")));
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
92
        reverse_btn.set_halign(gtk::Align::Center);
93
        reverse_btn.set_action_name(Some("win.reverse-colors"));
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
94
        let reverse_img = gtk::Image::from_icon_name(Some("network-transmit-receive-symbolic"), gtk::IconSize::Button);
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
95
96
97
98
99
100
101
        reverse_btn.set_image(Some(&reverse_img));

        let entries_container = gtk::Box::new(gtk::Orientation::Horizontal, 0);
        entries_container.pack_start(&self.bg_entry, false, false, 6);
        entries_container.pack_start(&reverse_btn, false, false, 6);
        entries_container.pack_start(&self.fg_entry, false, false, 6);
        entries_container.set_halign(gtk::Align::Center);
102
        entries_container.set_direction(gtk::TextDirection::Ltr);
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
103
104
105
106
107
108
109
        headerbar.set_custom_title(Some(&entries_container));

        let primary_menu = gio::Menu::new();
        primary_menu.append(Some(&gettext("_Keyboard Shortcuts")), Some("win.show-help-overlay"));
        primary_menu.append(Some(&gettext("_About Contrast")), Some("app.about"));

        let primary_btn = gtk::MenuButton::new();
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
110
        let primary_popover = gtk::Popover::from_model(Some(&primary_btn), &primary_menu);
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
111

Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
112
        let primary_img = gtk::Image::from_icon_name(Some("open-menu-symbolic"), gtk::IconSize::Button);
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
113
114
115
116
        primary_btn.add(&primary_img);
        primary_btn.set_popover(Some(&primary_popover));
        headerbar.pack_end(&primary_btn);

117
118
        // Main Container: Composed of ContrastPreview & ContrastLevelBar
        let container = gtk::Box::new(gtk::Orientation::Vertical, 0);
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
119
        container.set_property_margin(12);
120
121
122
        container.get_style_context().add_class("main-container");
        container.set_valign(gtk::Align::Center);

Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
123
124
125
        container.pack_start(&self.preview, true, true, 12);
        container.pack_start(&self.levelbar.borrow().widget, true, true, 12);

Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
126
127
128
129
        main_container.add(&container);

        self.handle.add(&main_container);
        self_.add(&self.handle);
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
130
131
132
133
134
135
136
137
138
139
140
141
142
143
        // Restore state
        self.state.borrow_mut().load_into(self_, &self.bg_entry, &self.fg_entry);
        self_.show_all();
    }
}

impl WidgetImpl for WindowPrivate {}

impl ContainerImpl for WindowPrivate {}

impl BinImpl for WindowPrivate {}

impl WindowImpl for WindowPrivate {}

Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
144
145
impl GtkApplicationWindowImpl for WindowPrivate {}

Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
146
impl ApplicationWindowImpl for WindowPrivate {}
Bilal Elmoussaoui's avatar
Init  
Bilal Elmoussaoui committed
147

Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
148
149
150
151
glib_wrapper! {
    pub struct Window(
        Object<subclass::simple::InstanceStruct<WindowPrivate>,
        subclass::simple::ClassStruct<WindowPrivate>,
152
        ApplicationWindowClass>)
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
153
        @extends gtk::Widget, gtk::Container, gtk::Bin, gtk::Window, libhandy::ApplicationWindow,  gtk::ApplicationWindow, gio::ActionMap;
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
154
155
156
157
158
159
160

    match fn {
        get_type => || WindowPrivate::get_type().to_glib(),
    }
}

impl Window {
161
    pub fn new(sender: glib::Sender<Action>, app: &Application) -> Self {
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
162
163
        let window = glib::Object::new(Window::static_type(), &[("application", app)]).unwrap().downcast::<Window>().unwrap();
        window.setup_signals(sender);
Bilal Elmoussaoui's avatar
Init  
Bilal Elmoussaoui committed
164
165
166
        window
    }

Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
167
    fn colour_changed(&self, bg_colour: String, fg_colour: String) {
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
168
169
170
        let self_ = WindowPrivate::from_instance(self);
        let bg_rgba = self_.state.borrow_mut().set_bg_colour(Some(&bg_colour));
        let fg_rgba = self_.state.borrow_mut().set_fg_colour(Some(&fg_colour));
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
171
172
        let contrast_level = colour::calc_contrast_level(&bg_rgba, &fg_rgba);

Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
173
174
175
176
177
        self_.levelbar.borrow_mut().set_contrast_level(contrast_level);
        self_.preview.set_contrast_level(contrast_level);
        if let Some(sender) = self_.sender.borrow().clone() {
            colour::compile_styles(&sender, fg_rgba, bg_rgba);
        }
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
178
179
    }

Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
180
181
182
    fn setup_signals(&self, sender: glib::Sender<Action>) {
        let self_ = WindowPrivate::from_instance(self);
        self_.sender.borrow_mut().replace(sender);
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
183
        // save window state on delete event
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
184
        self.connect_delete_event(clone!(@strong self_.state as state, @strong self as window => move |_, _| {
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
185
186
187
            if let Err(err) = state.borrow_mut().dump(&window) {
                warn!("Failed to save window state {}", err);
            }
188
            gtk::Inhibit(false)
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
189
        }));
Bilal Elmoussaoui's avatar
Init  
Bilal Elmoussaoui committed
190

Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
191
        let on_entry_changed = clone!(
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
192
193
                    @strong self_.fg_entry as fg_entry, @strong self_.bg_entry as bg_entry,
                    @strong self as window => move |_entry: &gtk::Entry| {
194
195
196
            let fg_colour = fg_entry.get_text();
            let bg_colour = bg_entry.get_text();

Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
197
198
            window.colour_changed(bg_colour, fg_colour);
        });
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
199

Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
200
201
        let bg_handle = self_.bg_entry.get_entry().connect_changed(on_entry_changed.clone());
        let fg_handle = self_.fg_entry.get_entry().connect_changed(on_entry_changed);
202

Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
203
        action!(
204
            self,
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
205
            "reverse-colors",
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
206
207
            clone!(@strong self_.fg_entry as fg_entry, @strong self_.bg_entry as bg_entry,
                    @strong self as window  => move |_, _| {
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
208
209
                fg_entry.get_entry().block_signal(&fg_handle);
                bg_entry.get_entry().block_signal(&bg_handle);
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
210
211
212
213
214
                let fg_colour = fg_entry.get_text();
                let bg_colour = bg_entry.get_text();
                fg_entry.set_text(Some(bg_colour.clone()));
                bg_entry.set_text(Some(fg_colour.clone()));

Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
215
216
                fg_entry.get_entry().unblock_signal(&fg_handle);
                bg_entry.get_entry().unblock_signal(&bg_handle);
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
217
218

                window.colour_changed(fg_colour, bg_colour);
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
219
            })
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
220
        );
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
221
        self.colour_changed(self_.bg_entry.get_text(), self_.fg_entry.get_text());
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
222
    }
Bilal Elmoussaoui's avatar
Init  
Bilal Elmoussaoui committed
223
}