grim/src/gui/screens/root.rs

88 lines
2.8 KiB
Rust
Raw Normal View History

2023-04-27 01:28:55 +03:00
// Copyright 2023 The Grim Developers
//
// 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 at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2023-04-27 21:20:22 +03:00
use std::cmp::min;
2023-05-04 20:09:26 +03:00
use eframe::epaint::{Color32, Shadow, Stroke};
2023-04-27 21:20:22 +03:00
use egui::style::Margin;
2023-05-04 20:09:26 +03:00
2023-04-27 21:20:22 +03:00
use crate::gui::{App, COLOR_YELLOW};
2023-05-04 20:09:26 +03:00
use crate::gui::app::is_dual_panel_mode;
2023-04-27 01:28:55 +03:00
use crate::gui::platform::PlatformCallbacks;
use crate::gui::screens::{Account, Accounts, Navigator, Screen, ScreenId};
2023-05-04 20:09:26 +03:00
use crate::gui::views::Network;
2023-04-27 01:28:55 +03:00
pub struct Root {
navigator: Navigator,
2023-04-27 02:05:16 +03:00
screens: Vec<Box<dyn Screen>>,
2023-05-04 20:09:26 +03:00
network: Network
2023-04-27 01:28:55 +03:00
}
impl Default for Root {
fn default() -> Self {
Self {
2023-04-27 21:20:22 +03:00
navigator: Navigator::default(),
2023-04-27 02:05:16 +03:00
screens: (vec![
2023-04-27 01:28:55 +03:00
Box::new(Accounts::default()),
Box::new(Account::default())
2023-04-27 21:20:22 +03:00
]),
2023-05-04 20:09:26 +03:00
network: Network::default()
2023-04-27 01:28:55 +03:00
}
}
}
2023-04-27 02:05:16 +03:00
impl Root {
2023-04-27 01:28:55 +03:00
fn id(&self) -> ScreenId {
ScreenId::Root
}
2023-05-04 20:09:26 +03:00
pub fn ui(&mut self, ui: &mut egui::Ui, frame: &mut eframe::Frame, cb: &dyn PlatformCallbacks) {
let is_network_panel_open = self.navigator.left_panel_open || is_dual_panel_mode(frame);
2023-04-27 21:20:22 +03:00
egui::SidePanel::left("network_panel")
.resizable(false)
2023-05-04 20:09:26 +03:00
.exact_width(if is_dual_panel_mode(frame) {
2023-04-27 21:20:22 +03:00
min(frame.info().window_info.size.x as i64, 400) as f32
} else {
frame.info().window_info.size.x
})
.frame(egui::Frame {
.. Default::default()
})
.show_animated_inside(ui, is_network_panel_open, |ui| {
2023-05-04 20:09:26 +03:00
self.network.ui(ui, frame, &mut self.navigator, cb);
2023-04-27 21:20:22 +03:00
});
2023-05-04 20:09:26 +03:00
egui::CentralPanel::default().frame(egui::Frame {
2023-04-27 21:20:22 +03:00
..Default::default()
}).show_inside(ui, |ui| {
self.show_current_screen(ui, frame, cb);
});
2023-04-27 02:05:16 +03:00
}
2023-05-04 20:09:26 +03:00
pub fn show_current_screen(&mut self,
ui: &mut egui::Ui,
frame: &mut eframe::Frame,
cb: &dyn PlatformCallbacks) {
2023-04-27 21:20:22 +03:00
let Self { navigator, screens, .. } = self;
2023-05-04 20:09:26 +03:00
let current = navigator.current();
2023-04-27 02:05:16 +03:00
for screen in screens.iter_mut() {
if screen.id() == *current {
2023-05-04 20:09:26 +03:00
screen.ui(ui, frame, navigator, cb);
2023-04-27 02:05:16 +03:00
break;
}
}
2023-04-27 01:28:55 +03:00
}
}