1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License in the LICENSE-APACHE file or at:
//     https://www.apache.org/licenses/LICENSE-2.0

//! Shared state

use std::num::NonZeroU32;
use std::task::Waker;

use super::{PendingAction, Platform, WindowSurface};
use kas::config::Options;
use kas::shell::Error;
use kas::theme::Theme;
use kas::util::warn_about_error;
use kas::{draw, AppData, ErasedStack, WindowId};
use std::cell::RefCell;
use std::rc::Rc;

#[cfg(feature = "clipboard")] use arboard::Clipboard;

/// Shell interface state
pub(super) struct ShellShared<Data: AppData, S: kas::draw::DrawSharedImpl, T> {
    pub(super) platform: Platform,
    #[cfg(feature = "clipboard")]
    clipboard: Option<Clipboard>,
    pub(super) draw: draw::SharedState<S>,
    pub(super) theme: T,
    pub(super) pending: Vec<PendingAction<Data>>,
    pub(super) waker: Waker,
    window_id: u32,
}

/// State shared between windows
pub struct SharedState<Data: AppData, S: WindowSurface, T> {
    pub(super) shell: ShellShared<Data, S::Shared, T>,
    pub(super) data: Data,
    pub(super) config: Rc<RefCell<kas::event::Config>>,
    /// Estimated scale factor (from last window constructed or available screens)
    pub(super) scale_factor: f64,
    options: Options,
}

impl<Data: AppData, S: WindowSurface, T: Theme<S::Shared>> SharedState<Data, S, T>
where
    T::Window: kas::theme::Window,
{
    /// Construct
    pub(super) fn new(
        data: Data,
        pw: super::PlatformWrapper,
        draw_shared: S::Shared,
        mut theme: T,
        options: Options,
        config: Rc<RefCell<kas::event::Config>>,
    ) -> Result<Self, Error> {
        let platform = pw.platform();
        let mut draw = kas::draw::SharedState::new(draw_shared, platform);
        theme.init(&mut draw);

        #[cfg(feature = "clipboard")]
        let clipboard = match Clipboard::new() {
            Ok(cb) => Some(cb),
            Err(e) => {
                warn_about_error("Failed to connect clipboard", &e);
                None
            }
        };

        Ok(SharedState {
            shell: ShellShared {
                platform,
                #[cfg(feature = "clipboard")]
                clipboard,
                draw,
                theme,
                pending: vec![],
                waker: pw.create_waker(),
                window_id: 0,
            },
            data,
            config,
            scale_factor: pw.guess_scale_factor(),
            options,
        })
    }

    #[inline]
    pub(crate) fn handle_messages(&mut self, messages: &mut ErasedStack) {
        if messages.reset_and_has_any() {
            let action = self.data.handle_messages(messages);
            self.shell.pending.push(PendingAction::Action(action));
        }
    }

    pub fn on_exit(&self) {
        match self
            .options
            .write_config(&self.config.borrow(), &self.shell.theme)
        {
            Ok(()) => (),
            Err(error) => warn_about_error("Failed to save config", &error),
        }
    }
}

impl<Data: AppData, S: kas::draw::DrawSharedImpl, T> ShellShared<Data, S, T> {
    /// Return the next window identifier
    ///
    /// TODO(opt): this should recycle used identifiers since WidgetId does not
    /// efficiently represent large numbers.
    pub fn next_window_id(&mut self) -> WindowId {
        let id = self.window_id + 1;
        self.window_id = id;
        WindowId::new(NonZeroU32::new(id).unwrap())
    }

    #[inline]
    pub fn get_clipboard(&mut self) -> Option<String> {
        #[cfg(feature = "clipboard")]
        {
            if let Some(cb) = self.clipboard.as_mut() {
                match cb.get_text() {
                    Ok(s) => return Some(s),
                    Err(e) => warn_about_error("Failed to get clipboard contents", &e),
                }
            }
        }

        None
    }

    #[inline]
    pub fn set_clipboard(&mut self, _content: String) {
        #[cfg(feature = "clipboard")]
        if let Some(cb) = self.clipboard.as_mut() {
            match cb.set_text(_content) {
                Ok(()) => (),
                Err(e) => warn_about_error("Failed to set clipboard contents", &e),
            }
        }
    }

    #[inline]
    pub fn get_primary(&mut self) -> Option<String> {
        #[cfg(all(
            unix,
            not(any(target_os = "macos", target_os = "android", target_os = "emscripten")),
            feature = "clipboard",
        ))]
        {
            use arboard::{GetExtLinux, LinuxClipboardKind};
            if let Some(cb) = self.clipboard.as_mut() {
                match cb.get().clipboard(LinuxClipboardKind::Primary).text() {
                    Ok(s) => return Some(s),
                    Err(e) => warn_about_error("Failed to get clipboard contents", &e),
                }
            }
        }

        None
    }

    #[inline]
    pub fn set_primary(&mut self, _content: String) {
        #[cfg(all(
            unix,
            not(any(target_os = "macos", target_os = "android", target_os = "emscripten")),
            feature = "clipboard",
        ))]
        if let Some(cb) = self.clipboard.as_mut() {
            use arboard::{LinuxClipboardKind, SetExtLinux};
            match cb
                .set()
                .clipboard(LinuxClipboardKind::Primary)
                .text(_content)
            {
                Ok(()) => (),
                Err(e) => warn_about_error("Failed to set clipboard contents", &e),
            }
        }
    }
}