aboutsummaryrefslogtreecommitdiff
path: root/src/file_registry.rs
blob: 188e1fe8d897e7dd6c98b38471104e1056986262 (plain)
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
// Map, wardley map editor for linux
// Copyright (C) 2026 Rubén Beltrán del Río

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.

// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//! Manages tracking of open files and window controllers.
//!
//! GTK is single-threaded, so thread-local storage is safe here.

use std::cell::RefCell;
use std::path::PathBuf;

use relm4::component::Controller;
use relm4::{adw};

use crate::AppModel;
use crate::actions::Action;

thread_local! {
    /// Keeps window controllers alive for the lifetime of each window.
    static WINDOW_CONTROLLERS: RefCell<Vec<Controller<AppModel>>> = const { RefCell::new(Vec::new()) };

    /// Tracks which files are currently open to prevent duplicate windows.
    static OPEN_FILES: RefCell<Vec<(PathBuf, adw::Window)>> = const { RefCell::new(Vec::new()) };

    /// Stores senders for all windows (including main window) to enable broadcasting.
    static WINDOW_SENDERS: RefCell<Vec<relm4::Sender<Action>>> = const { RefCell::new(Vec::new()) };
}

/// Stores a window controller to keep it alive.
pub fn store_controller(controller: Controller<AppModel>) {
    WINDOW_CONTROLLERS.with_borrow_mut(|controllers| {
        controllers.push(controller);
    });
}

/// Returns the window that has the given file open, if any.
pub fn find_window_for_file(path: &PathBuf) -> Option<adw::Window> {
    OPEN_FILES.with_borrow(|files| {
        files
            .iter()
            .find(|(file_path, _)| file_path == path)
            .map(|(_, window)| window.clone())
    })
}

/// Registers a file as being open in the given window.
pub fn register_file(path: &PathBuf, window: &adw::Window) {
    OPEN_FILES.with_borrow_mut(|files| {
        if !files.iter().any(|(file_path, _)| file_path == path) {
            files.push((path.clone(), window.clone()));
        }
    });
}

/// Unregisters a file when its window is closed.
pub fn unregister_file(path: &PathBuf) {
    OPEN_FILES.with_borrow_mut(|files| {
        files.retain(|(file_path, _)| file_path != path);
    });
}

/// Registers a window's sender for broadcasting.
pub fn register_sender(sender: relm4::Sender<Action>) {
    WINDOW_SENDERS.with_borrow_mut(|senders| {
        senders.push(sender);
    });
}

/// Broadcasts a message to all windows via their registered senders.
pub fn broadcast_to_all_windows(message: &Action) {
    WINDOW_SENDERS.with_borrow(|senders| {
        for sender in senders {
            sender.emit(message.clone());
        }
    });
}