Yoda 0.11.9

Browser for Gemini Protocol
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
mod action;
mod database;
mod item;
mod menu;

use super::{Action as WindowAction, Position};
use crate::Profile;
use action::Action;
use adw::{TabPage, TabView};
use anyhow::Result;
use gtk::{Box, Orientation, gio::Icon, glib::Propagation, prelude::ActionExt};
pub use item::Item;
use menu::Menu;
use sourceview::prelude::IsA;
use sqlite::Transaction;
use std::{cell::RefCell, collections::HashMap, rc::Rc, sync::Arc};

// Main
pub struct Tab {
    window_action: Rc<WindowAction>,
    profile: Arc<Profile>,
    index: Rc<RefCell<HashMap<TabPage, Rc<Item>>>>,
    pub action: Rc<Action>,
    pub tab_view: TabView,
}

impl Tab {
    // Constructors

    /// Build new `Self`
    pub fn build(profile: &Arc<Profile>, window_action: &Rc<WindowAction>) -> Self {
        let action = Rc::new(Action::new());

        // Init empty HashMap index
        let index = Rc::new(RefCell::new(HashMap::new()));

        // Init model
        let tab_view = TabView::builder()
            .menu_model(&gtk::gio::Menu::menu(window_action))
            .build();

        // Change default icon (if available in the system icon set)
        // * visible for pinned tabs only
        // * @TODO not default GTK behavior, make this feature optional
        if let Ok(default_icon) = Icon::for_string("view-pin-symbolic") {
            tab_view.set_default_icon(&default_icon);
        }

        // Init events
        tab_view.connect_setup_menu({
            let index = index.clone();
            let window_action = window_action.clone();
            move |tab_view, tab_page| {
                // by documentation:
                // * `tab_page` == `Some` - popover open
                // * `tab_page` == `None` - popover closed
                update_actions(
                    tab_view,
                    match tab_page {
                        Some(this) => Some(this.clone()),
                        None => tab_view.selected_page(),
                    }
                    .as_ref(),
                    &index,
                    &window_action,
                );
            }
        });

        tab_view.connect_close_page({
            let index = index.clone();
            let profile = profile.clone();
            let window_action = window_action.clone();
            move |tab_view, tab_page| {
                // remove closed Item from Tab index
                if let Some(item) = index.borrow_mut().remove(tab_page) {
                    // keep removed `Item` reference in the memory (to reopen from the main menu)
                    // * skip item with blank request
                    if !item.page.navigation.request().is_empty() {
                        profile.history.close(&item.page.navigation.request());
                    }
                }
                // reassign global actions to active tab
                update_actions(
                    tab_view,
                    tab_view.selected_page().as_ref(),
                    &index,
                    &window_action,
                );
                Propagation::Proceed
            }
        });

        tab_view.connect_page_reordered({
            let window_action = window_action.clone();
            let index = index.clone();
            move |tab_view, tab_page, _| {
                update_actions(tab_view, Some(tab_page), &index, &window_action)
            }
        });

        tab_view.connect_selected_page_notify({
            let window_action = window_action.clone();
            let index = index.clone();
            move |tab_view| {
                update_actions(
                    tab_view,
                    match tab_view.selected_page() {
                        Some(this) => {
                            this.set_needs_attention(false);
                            Some(this.clone())
                        }
                        None => None,
                    }
                    .as_ref(),
                    &index,
                    &window_action,
                )
            }
        });

        // Return activated `Self`
        Self {
            profile: profile.clone(),
            window_action: window_action.clone(),
            index,
            tab_view,
            action,
        }
    }

    // Actions
    pub fn append(
        &self,
        position: Position,
        request: Option<&str>,
        is_pinned: bool,
        is_selected: bool,
        is_needs_attention: bool,
        is_load: bool,
    ) -> Rc<Item> {
        // Generate new `TabPage` with blank `Widget`
        let (tab_page, target_child) = new_tab_page(&self.tab_view, position);

        // Init new tab item
        let item = Rc::new(Item::build(
            (&tab_page, &target_child),
            &self.profile,
            // Actions
            (&self.window_action, &self.action),
            // Options
            request,
            is_load,
        ));

        // Make initial setup
        item.page.set_needs_attention(is_needs_attention);
        item.page.set_title("New page");

        // Expect user input on tab appended has empty request entry
        // * this action initiated here because should be applied on tab appending event only
        if request.is_none() || request.is_some_and(|value| value.is_empty()) {
            item.page.navigation.grab_focus();
        }

        // Relate with GTK `TabPage` with app `Item`
        self.index
            .borrow_mut()
            .insert(item.tab_page.clone(), item.clone());

        // Setup
        self.tab_view.set_page_pinned(&item.tab_page, is_pinned);
        if is_selected {
            self.tab_view.set_selected_page(&item.tab_page);
        }

        // Forcefully update global actions on HashMap index build complete
        // * `selected_page_notify` runs this action also, just before Item init @TODO
        update_actions(
            &self.tab_view,
            self.tab_view.selected_page().as_ref(),
            &self.index,
            &self.window_action,
        );

        item
    }

    /// Close page at given `position`, `None` to close selected page (if available)
    /// * this action includes `pinned` pages, to prevent that:
    ///   * deactivate [SimpleAction](https://docs.gtk.org/gio/class.SimpleAction.html) outside if selected page should not be closed
    ///   * use native [TabView](https://gnome.pages.gitlab.gnome.org/libadwaita/doc/main/class.TabView.html) API with `GObject` reference getter
    pub fn close(&self, page_position: Option<i32>) {
        if let Some(page) = match page_position {
            Some(value) => Some(self.tab_view.nth_page(value)),
            None => self.tab_view.selected_page(),
        } {
            self.tab_view.set_page_pinned(&page, false);
            self.tab_view.close_page(&page);
        }
    }

    /// Close all pages
    /// * this action includes `pinned` pages, to prevent that:
    ///   * deactivate [SimpleAction](https://docs.gtk.org/gio/class.SimpleAction.html) outside if selected page should not be closed
    ///   * use native [TabView](https://gnome.pages.gitlab.gnome.org/libadwaita/doc/main/class.TabView.html) API with `GObject` reference getter
    pub fn close_all(&self) {
        while let Some(page) = self.tab_view.selected_page() {
            self.tab_view.set_page_pinned(&page, false);
            self.tab_view.close_page(&page);
        }
    }

    // Toggle search widget
    pub fn find(&self, page_position: Option<i32>) {
        if let Some(item) = self.item(page_position) {
            item.page.find();
        }
    }

    // Save page at given `position`, `None` to save selected page (if available)
    pub fn save_as(&self, page_position: Option<i32>) {
        if let Some(item) = self.item(page_position) {
            item.page.navigation.to_download();
            self.window_action.reload.activate();
        }
    }

    // View source for page at given `position`, `None` to use selected page (if available)
    pub fn source(&self, page_position: Option<i32>) {
        if let Some(item) = self.item(page_position) {
            item.page.navigation.to_source();
            self.window_action.reload.activate();
        }
    }

    /// Toggle `Bookmark` for `Page` at given `position` (current page on `None`)
    pub fn bookmark(&self, page_position: Option<i32>) {
        if let Some(item) = self.item(page_position) {
            item.page.bookmark()
        }
    }

    /// Toggle pin for page at given `position`, `None` to pin selected page (if available)
    pub fn pin(&self, page_position: Option<i32>) {
        if let Some(page) = match page_position {
            Some(value) => Some(self.tab_view.nth_page(value)),
            None => self.tab_view.selected_page(),
        } {
            self.tab_view.set_page_pinned(&page, !page.is_pinned()); // toggle
        }
    }

    pub fn home(&self, page_position: Option<i32>) {
        if let Some(item) = self.item(page_position) {
            item.action.home.activate(None)
        }
    }

    pub fn history_back(&self, page_position: Option<i32>) {
        if let Some(item) = self.item(page_position) {
            item.action.history.back.activate(None)
        }
    }

    pub fn history_forward(&self, page_position: Option<i32>) {
        if let Some(item) = self.item(page_position) {
            item.action.history.forward.activate(None)
        }
    }

    /// Reload page at `i32` position or selected page on `None` given
    pub fn reload(&self, page_position: Option<i32>) {
        if let Some(item) = self.item(page_position) {
            item.client
                .handle(&item.page.navigation.request(), true, false);
        }
    }

    pub fn open(&self, page_position: Option<i32>, request: &str) {
        if let Some(item) = self.item(page_position) {
            item.action.load.activate(Some(request), true, false);
        }
    }

    pub fn clean(&self, transaction: &Transaction, app_browser_window_id: i64) -> Result<()> {
        for record in database::select(transaction, app_browser_window_id)? {
            database::delete(transaction, record.id)?;
            // Delegate clean action to childs
            for (_, item) in self.index.borrow().iter() {
                item.clean(transaction, record.id)?
            }
        }
        Ok(())
    }

    pub fn restore(&self, transaction: &Transaction, app_browser_window_id: i64) -> Result<()> {
        for tab_record in database::select(transaction, app_browser_window_id)? {
            for item_record in item::restore(transaction, tab_record.id)? {
                // Generate new `TabPage` with blank `Widget`
                let (tab_page, target_child) =
                    new_tab_page(&self.tab_view, Position::Number(item_record.page_position));

                // Init new tab item
                let item = Rc::new(Item::build(
                    (&tab_page, &target_child),
                    &self.profile,
                    // Actions
                    (&self.window_action, &self.action),
                    // Options
                    None,
                    false,
                ));

                // Relate with GTK `TabPage` with app `Item`
                self.index
                    .borrow_mut()
                    .insert(item.tab_page.clone(), item.clone());

                // Setup
                self.tab_view
                    .set_page_pinned(&item.tab_page, item_record.is_pinned);

                if item_record.is_selected {
                    self.tab_view.set_selected_page(&item.tab_page);
                }

                // Forcefully update global actions on HashMap index build complete
                // * `selected_page_notify` runs this action also, just before Item init @TODO
                update_actions(
                    &self.tab_view,
                    self.tab_view.selected_page().as_ref(),
                    &self.index,
                    &self.window_action,
                );

                // Restore children components
                item.page.restore(transaction, item_record.id)?;
            }
        }
        Ok(())
    }

    pub fn save(&self, transaction: &Transaction, app_browser_window_id: i64) -> Result<()> {
        let id = database::insert(transaction, app_browser_window_id)?;
        for (_, item) in self.index.borrow().iter() {
            item.save(transaction, id, self.tab_view.page_position(&item.tab_page))?;
        }
        Ok(())
    }

    pub fn init(&self) {
        // Append just one blank page if no tabs available after last session restore
        if self.index.borrow().is_empty() {
            self.append(Position::End, None, false, true, false, false);
        }

        // @TODO other/child features..
    }

    /// Find `Item` by `TabPage` position in HashMap `index`
    fn item(&self, page_position: Option<i32>) -> Option<Rc<Item>> {
        match page_position {
            Some(value) => Some(self.tab_view.nth_page(value)),
            None => self.tab_view.selected_page(),
        }
        .map(|tab_page| self.index.borrow().get(&tab_page).unwrap().clone())
    }
}

// Tools

pub fn migrate(tx: &Transaction) -> Result<()> {
    // Migrate self components
    database::init(tx)?;

    // Delegate migration to childs
    item::migrate(tx)?;

    // Success
    Ok(())
}

/// Update global actions for given [TabPage](https://gnome.pages.gitlab.gnome.org/libadwaita/doc/main/class.TabPage.html)
/// using `Item` match relation in the HashMap `index`
fn update_actions(
    tab_view: &TabView,
    tab_page: Option<&TabPage>,
    index: &Rc<RefCell<HashMap<TabPage, Rc<Item>>>>,
    window_action: &Rc<WindowAction>,
) {
    if let Some(tab_page) = tab_page {
        if let Some(item) = index.borrow().get(tab_page) {
            window_action
                .home
                .simple_action
                .set_enabled(item.action.home.is_enabled());
            window_action
                .reload
                .simple_action
                .set_enabled(item.action.reload.is_enabled());
            window_action
                .history_back
                .simple_action
                .set_enabled(item.action.history.back.is_enabled());
            window_action
                .history_forward
                .simple_action
                .set_enabled(item.action.history.forward.is_enabled());
            window_action
                .save_as
                .simple_action
                .set_enabled(!item.page.navigation.is_file());

            window_action.change_state(Some(tab_view.page_position(tab_page)));
            return;
        } // @TODO `connect_selected_page_notify` panics on unwrap
    }
    // Reset to defaults
    window_action.home.simple_action.set_enabled(false);
    window_action.reload.simple_action.set_enabled(false);
    window_action.history_back.simple_action.set_enabled(false);
    window_action
        .history_forward
        .simple_action
        .set_enabled(false);

    window_action.change_state(None);
}

/// Create new [TabPage](https://gnome.pages.gitlab.gnome.org/libadwaita/doc/main/class.TabPage.html)
/// in [TabView](https://gnome.pages.gitlab.gnome.org/libadwaita/doc/main/class.TabView.html)
/// with given child Widget at given position
///
/// * if the `position` match pinned tab, GTK will panic with notice:
///   adw_tab_view_insert: assertion 'position >= self->n_pinned_pages'\
///   this shared method prepends new page after pinned tabs as the solution
fn add_tab_page(tab_view: &TabView, child: &impl IsA<gtk::Widget>, position: i32) -> TabPage {
    if position > tab_view.n_pinned_pages() {
        tab_view.insert(child, position)
    } else {
        tab_view.prepend(child)
    }
}

/// Create new [TabPage](https://gnome.pages.gitlab.gnome.org/libadwaita/doc/main/class.TabPage.html)
/// in [TabView](https://gnome.pages.gitlab.gnome.org/libadwaita/doc/main/class.TabView.html) at app `Position`
fn new_tab_page(tab_view: &TabView, position: Position) -> (TabPage, Box) {
    let child = Box::builder().orientation(Orientation::Vertical).build();
    (
        match position {
            Position::After => match tab_view.selected_page() {
                Some(selected_page) => {
                    add_tab_page(tab_view, &child, tab_view.page_position(&selected_page) + 1)
                }
                None => tab_view.append(&child),
            },
            Position::End => tab_view.append(&child),
            Position::Number(value) => add_tab_page(tab_view, &child, value),
        },
        child,
    )
}