window.rs 4.17 KB
Newer Older
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
1
use crate::colour_entry::ColourEntry;
Bilal Elmoussaoui's avatar
Init  
Bilal Elmoussaoui committed
2
use crate::config::{APP_ID, PROFILE};
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
3
4
use crate::contrast_level::{ContrastLevel, ContrastLevelBar};
use crate::contrast_preview::ContrastPreview;
Bilal Elmoussaoui's avatar
Init  
Bilal Elmoussaoui committed
5
use crate::window_state;
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
6

Bilal Elmoussaoui's avatar
Init  
Bilal Elmoussaoui committed
7
8
9
10
11
use gtk::prelude::*;

pub struct Window {
    pub widget: gtk::ApplicationWindow,
    appmenu_button: gtk::MenuButton,
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
12
13
    fg_entry: ColourEntry,
    bg_entry: ColourEntry,
Bilal Elmoussaoui's avatar
Init  
Bilal Elmoussaoui committed
14
15
16
17
18
}

impl Window {
    pub fn new() -> Self {
        let settings = gio::Settings::new(APP_ID);
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
19
        let builder = gtk::Builder::new_from_resource("/org/gnome/design/ColourContrast/window.ui");
Bilal Elmoussaoui's avatar
Init  
Bilal Elmoussaoui committed
20
21
22
        let widget: gtk::ApplicationWindow = builder.get_object("window").unwrap();
        let appmenu_button: gtk::MenuButton = builder.get_object("appmenu_button").unwrap();

Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
23
24
25
        let bg_entry = ColourEntry::new("Background Colour".to_string(), &widget);
        let fg_entry = ColourEntry::new("Foreground Colour".to_string(), &widget);

Bilal Elmoussaoui's avatar
Init  
Bilal Elmoussaoui committed
26
27
28
29
        if PROFILE == "Devel" {
            widget.get_style_context().add_class("devel");
        }

Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
30
31
32
33
34
35
        let window = Window {
            widget,
            appmenu_button,
            fg_entry,
            bg_entry,
        };
Bilal Elmoussaoui's avatar
Init  
Bilal Elmoussaoui committed
36
37

        window.init(settings);
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
38
        window.setup_widgets();
Bilal Elmoussaoui's avatar
Init  
Bilal Elmoussaoui committed
39
40
41
42
43
44
        window
    }

    pub fn init(&self, settings: gio::Settings) {
        // setup app menu

Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
45
        let menu_builder = gtk::Builder::new_from_resource("/org/gnome/design/ColourContrast/menu.ui");
Bilal Elmoussaoui's avatar
Init  
Bilal Elmoussaoui committed
46
47
48
49
50
51
52
53
54
55
56
57
        let popover_menu: gio::MenuModel = menu_builder.get_object("popover_menu").unwrap();
        self.appmenu_button.set_menu_model(Some(&popover_menu));
        // load latest window state
        window_state::load(&self.widget, &settings);

        // save window state on delete event
        self.widget.connect_delete_event(move |window, _| {
            window_state::save(&window, &settings);
            Inhibit(false)
        });
        // Init the widgets

Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
58
        self.widget.show();
Bilal Elmoussaoui's avatar
Init  
Bilal Elmoussaoui committed
59
    }
Bilal Elmoussaoui's avatar
Bilal Elmoussaoui committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117

    fn setup_widgets(&self) {
        let container = gtk::Box::new(gtk::Orientation::Vertical, 0);
        container.show();

        let reverse_btn = gtk::Button::new();
        let reverse_img = gtk::Image::new_from_icon_name(Some("network-transmit-receive-symbolic"), gtk::IconSize::Button);
        reverse_img.show();
        reverse_btn.set_image(Some(&reverse_img));
        reverse_btn.show();
        let fg_entry = self.fg_entry.clone();
        let bg_entry = self.bg_entry.clone();
        reverse_btn.connect_clicked(move |button| {
            let fg_colour = fg_entry.get_text();
            let bg_colour = bg_entry.get_text();
            bg_entry.set_text(Some(fg_colour));
            fg_entry.set_text(Some(bg_colour));
        });

        let entries_container = gtk::Box::new(gtk::Orientation::Horizontal, 6);
        entries_container.pack_start(&self.bg_entry.widget, false, false, 6);
        entries_container.pack_start(&reverse_btn, false, false, 6);
        entries_container.pack_start(&self.fg_entry.widget, false, false, 6);
        entries_container.set_halign(gtk::Align::Center);
        entries_container.set_valign(gtk::Align::Center);
        entries_container.set_vexpand(true);
        entries_container.show();

        let bottom_bar = gtk::Box::new(gtk::Orientation::Vertical, 0);
        bottom_bar.get_style_context().add_class("sidebar");
        bottom_bar.add(&entries_container);
        bottom_bar.show();

        let preview = ContrastPreview::new();
        let levelbar = ContrastLevelBar::new();

        container.pack_start(&preview.widget, true, true, 0);
        container.pack_start(&levelbar.widget, false, false, 0);
        container.pack_end(&bottom_bar, false, false, 0);

        let previewer = preview.clone();
        self.bg_entry.entry.connect_changed(move |entry| {
            let colour = entry.get_text().unwrap();
            let colour = colour.as_str();
            previewer.update_bg_colour(Some(colour));
        });

        let previewer = preview.clone();
        self.fg_entry.entry.connect_changed(move |entry| {
            let colour = entry.get_text().unwrap();
            let colour = colour.as_str();
            previewer.update_fg_colour(Some(colour));
        });

        self.widget.add(&container);
    }

    fn setup_signals(&self) {}
Bilal Elmoussaoui's avatar
Init  
Bilal Elmoussaoui committed
118
}