2023-04-12 21:52:35 +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-07-14 00:55:31 +03:00
|
|
|
use std::sync::atomic::{AtomicI32, Ordering};
|
|
|
|
|
|
|
|
use egui::Context;
|
|
|
|
use lazy_static::lazy_static;
|
2023-04-27 01:28:55 +03:00
|
|
|
|
2023-07-14 00:55:31 +03:00
|
|
|
use crate::gui::Colors;
|
2023-04-27 01:28:55 +03:00
|
|
|
use crate::gui::platform::PlatformCallbacks;
|
2023-07-14 00:55:31 +03:00
|
|
|
use crate::gui::views::Root;
|
2023-04-19 02:14:39 +03:00
|
|
|
|
2023-07-14 00:55:31 +03:00
|
|
|
/// Implements ui entry point and contains platform-specific callbacks.
|
2023-04-18 16:19:43 +03:00
|
|
|
pub struct PlatformApp<Platform> {
|
2023-07-14 00:55:31 +03:00
|
|
|
/// Platform specific callbacks handler.
|
2023-04-18 16:19:43 +03:00
|
|
|
pub(crate) platform: Platform,
|
2023-07-13 03:54:27 +03:00
|
|
|
/// Main ui content.
|
2023-07-14 00:55:31 +03:00
|
|
|
root: Root
|
2023-07-11 23:36:48 +03:00
|
|
|
}
|
|
|
|
|
2023-07-14 00:55:31 +03:00
|
|
|
impl<Platform> PlatformApp<Platform> {
|
|
|
|
pub fn new(platform: Platform) -> Self {
|
|
|
|
Self { platform, root: Root::default() }
|
2023-07-11 23:36:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-14 00:55:31 +03:00
|
|
|
impl<Platform: PlatformCallbacks> eframe::App for PlatformApp<Platform> {
|
|
|
|
fn update(&mut self, ctx: &Context, frame: &mut eframe::Frame) {
|
|
|
|
// Show panels to support display cutouts (insets).
|
|
|
|
padding_panels(ctx);
|
2023-07-13 03:54:27 +03:00
|
|
|
|
2023-07-14 00:55:31 +03:00
|
|
|
// Show main content.
|
2023-04-22 21:16:17 +03:00
|
|
|
egui::CentralPanel::default()
|
2023-06-02 02:05:34 +03:00
|
|
|
.frame(egui::Frame {
|
2023-06-03 11:22:51 +03:00
|
|
|
fill: Colors::FILL,
|
2023-07-03 21:17:49 +03:00
|
|
|
..Default::default()
|
2023-04-21 23:59:59 +03:00
|
|
|
})
|
2023-04-22 21:16:17 +03:00
|
|
|
.show(ctx, |ui| {
|
2023-07-14 00:55:31 +03:00
|
|
|
self.root.ui(ui, frame, &self.platform);
|
2023-06-02 21:19:34 +03:00
|
|
|
});
|
2023-04-19 02:14:39 +03:00
|
|
|
}
|
2023-04-21 03:45:59 +03:00
|
|
|
|
2023-07-14 00:55:31 +03:00
|
|
|
fn on_close_event(&mut self) -> bool {
|
|
|
|
Root::show_exit_modal();
|
|
|
|
self.root.exit_allowed
|
2023-07-11 23:36:48 +03:00
|
|
|
}
|
2023-07-14 00:55:31 +03:00
|
|
|
}
|
2023-07-11 23:36:48 +03:00
|
|
|
|
2023-07-14 00:55:31 +03:00
|
|
|
/// Draw panels to support display cutouts (insets).
|
|
|
|
fn padding_panels(ctx: &Context) {
|
|
|
|
egui::TopBottomPanel::top("top_padding_panel")
|
|
|
|
.frame(egui::Frame {
|
|
|
|
inner_margin: egui::style::Margin::same(0.0),
|
|
|
|
fill: Colors::YELLOW,
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.show_separator_line(false)
|
|
|
|
.resizable(false)
|
|
|
|
.exact_height(get_top_display_cutout())
|
|
|
|
.show(ctx, |_ui| {});
|
|
|
|
|
|
|
|
egui::TopBottomPanel::bottom("bottom_padding_panel")
|
|
|
|
.frame(egui::Frame {
|
|
|
|
inner_margin: egui::style::Margin::same(0.0),
|
|
|
|
fill: Colors::BLACK,
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.show_separator_line(false)
|
|
|
|
.resizable(false)
|
|
|
|
.exact_height(get_bottom_display_cutout())
|
|
|
|
.show(ctx, |_ui| {});
|
|
|
|
|
|
|
|
egui::SidePanel::right("right_padding_panel")
|
|
|
|
.frame(egui::Frame {
|
|
|
|
inner_margin: egui::style::Margin::same(0.0),
|
|
|
|
fill: Colors::YELLOW,
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.show_separator_line(false)
|
|
|
|
.resizable(false)
|
|
|
|
.max_width(get_right_display_cutout())
|
|
|
|
.show(ctx, |_ui| {});
|
|
|
|
|
|
|
|
egui::SidePanel::left("left_padding_panel")
|
|
|
|
.frame(egui::Frame {
|
|
|
|
inner_margin: egui::style::Margin::same(0.0),
|
|
|
|
fill: Colors::YELLOW,
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.show_separator_line(false)
|
|
|
|
.resizable(false)
|
|
|
|
.max_width(get_left_display_cutout())
|
|
|
|
.show(ctx, |_ui| {});
|
|
|
|
}
|
2023-06-02 22:55:07 +03:00
|
|
|
|
2023-07-14 00:55:31 +03:00
|
|
|
/// Get top display cutout (inset) size.
|
|
|
|
pub fn get_top_display_cutout() -> f32 {
|
|
|
|
TOP_DISPLAY_CUTOUT.load(Ordering::Relaxed) as f32
|
|
|
|
}
|
2023-06-02 22:55:07 +03:00
|
|
|
|
2023-07-14 00:55:31 +03:00
|
|
|
/// Get right display cutout (inset) size.
|
|
|
|
pub fn get_right_display_cutout() -> f32 {
|
|
|
|
RIGHT_DISPLAY_CUTOUT.load(Ordering::Relaxed) as f32
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get bottom display cutout (inset) size.
|
|
|
|
pub fn get_bottom_display_cutout() -> f32 {
|
|
|
|
BOTTOM_DISPLAY_CUTOUT.load(Ordering::Relaxed) as f32
|
|
|
|
}
|
2023-05-04 20:09:26 +03:00
|
|
|
|
2023-07-14 00:55:31 +03:00
|
|
|
/// Get left display cutout (inset) size.
|
|
|
|
pub fn get_left_display_cutout() -> f32 {
|
|
|
|
LEFT_DISPLAY_CUTOUT.load(Ordering::Relaxed) as f32
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Fields to handle platform-specific display cutouts (insets).
|
|
|
|
lazy_static! {
|
|
|
|
static ref TOP_DISPLAY_CUTOUT: AtomicI32 = AtomicI32::new(0);
|
|
|
|
static ref RIGHT_DISPLAY_CUTOUT: AtomicI32 = AtomicI32::new(0);
|
|
|
|
static ref BOTTOM_DISPLAY_CUTOUT: AtomicI32 = AtomicI32::new(0);
|
|
|
|
static ref LEFT_DISPLAY_CUTOUT: AtomicI32 = AtomicI32::new(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
#[cfg(target_os = "android")]
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
#[no_mangle]
|
|
|
|
/// Callback from Java code to update display cutouts (insets).
|
|
|
|
pub extern "C" fn Java_mw_gri_android_MainActivity_onDisplayCutouts(
|
|
|
|
_env: jni::JNIEnv,
|
|
|
|
_class: jni::objects::JObject,
|
|
|
|
cutouts: jni::sys::jarray
|
|
|
|
) {
|
|
|
|
use jni::objects::{JObject, JPrimitiveArray};
|
|
|
|
|
|
|
|
let mut array: [i32; 4] = [0; 4];
|
|
|
|
unsafe {
|
|
|
|
let j_obj = JObject::from_raw(cutouts);
|
|
|
|
let j_arr = JPrimitiveArray::from(j_obj);
|
|
|
|
_env.get_int_array_region(j_arr, 0, array.as_mut()).unwrap();
|
2023-07-13 03:54:27 +03:00
|
|
|
}
|
2023-07-14 00:55:31 +03:00
|
|
|
TOP_DISPLAY_CUTOUT.store(array[0], Ordering::Relaxed);
|
|
|
|
RIGHT_DISPLAY_CUTOUT.store(array[1], Ordering::Relaxed);
|
|
|
|
BOTTOM_DISPLAY_CUTOUT.store(array[2], Ordering::Relaxed);
|
|
|
|
LEFT_DISPLAY_CUTOUT.store(array[3], Ordering::Relaxed);
|
2023-06-13 23:45:03 +03:00
|
|
|
}
|
2023-07-14 00:55:31 +03:00
|
|
|
|