gui: fix button touch, add icons, optimize screen panels (in progress)

This commit is contained in:
ardocrat
2023-04-21 03:45:59 +03:00
parent a5ba9660b3
commit c572c3926e
55 changed files with 1083 additions and 448 deletions
+126 -96
View File
@@ -12,10 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use egui::{Color32, FontTweak, Visuals};
use egui::epaint::Shadow;
use jni::objects::{JObject, JPrimitiveArray};
use winit::platform::android::activity::AndroidApp;
use crate::gui::{PlatformApp, PlatformCallbacks};
@@ -23,9 +19,9 @@ use crate::gui::app::Screens;
#[derive(Clone)]
pub struct Android {
pub android_app: AndroidApp,
pub cutouts: [i32; 4],
pub window_size: [f32; 2]
android_app: AndroidApp,
cutouts: [i32; 4],
window_size: [f32; 2]
}
impl Android {
@@ -58,32 +54,80 @@ impl PlatformCallbacks for Android {
impl PlatformApp<Android> {
pub fn new(cc: &eframe::CreationContext<'_>, platform: Android) -> Self {
setup_fonts(&cc.egui_ctx);
Self::setup_visuals(&cc.egui_ctx);
Self::setup_fonts(&cc.egui_ctx);
Self {
screens: Screens::default(),
platform,
}
}
}
fn setup_fonts(ctx: &egui::Context) {
let mut fonts = egui::FontDefinitions::default();
fonts.font_data.insert(
"roboto".to_owned(),
egui::FontData::from_static(include_bytes!(
"../../../../fonts/roboto.ttf"
)).tweak(FontTweak {
scale: 1.0,
y_offset_factor: -0.20,
y_offset: 0.0
}),
);
fonts
.families
.entry(egui::FontFamily::Proportional)
.or_default()
.insert(0, "roboto".to_owned());
ctx.set_fonts(fonts);
fn setup_visuals(ctx: &egui::Context) {
ctx.set_visuals(egui::Visuals::light());
}
fn setup_fonts(ctx: &egui::Context) {
use egui::FontFamily::Proportional;
let mut fonts = egui::FontDefinitions::default();
fonts.font_data.insert(
"roboto".to_owned(),
egui::FontData::from_static(include_bytes!(
"../../../../fonts/roboto.ttf"
)).tweak(egui::FontTweak {
scale: 1.0,
y_offset_factor: -0.20,
y_offset: 0.0
}),
);
fonts
.families
.entry(Proportional)
.or_default()
.insert(0, "roboto".to_owned());
ctx.set_fonts(fonts);
use egui::FontId;
use egui::TextStyle::*;
let mut style = (*ctx.style()).clone();
style.text_styles = [
(Heading, FontId::new(24.0, Proportional)),
(Body, FontId::new(18.0, Proportional)),
(Monospace, FontId::new(18.0, Proportional)),
(Button, FontId::new(18.0, Proportional)),
(Small, FontId::new(12.0, Proportional)),
].into();
ctx.set_style(style);
}
fn get_display_cutouts(app: &AndroidApp) -> [i32; 4] {
use jni::objects::{JObject, JPrimitiveArray};
let vm = unsafe { jni::JavaVM::from_raw(app.vm_as_ptr() as _) }.unwrap();
let mut env = vm.attach_current_thread().unwrap();
let activity = unsafe {
JObject::from_raw(app.activity_as_ptr() as jni::sys::jobject)
};
let _res = env
.call_method(
activity,
"getDisplayCutouts",
"()[I",
&[],
)
.unwrap();
let mut array: [i32; 4] = [0; 4];
let object: jni::sys::jobject = unsafe { _res.as_jni().l };
unsafe {
env.get_int_array_region(JPrimitiveArray::from(
JObject::from_raw(object)), 0, array.as_mut()
).unwrap();
}
array
}
}
impl eframe::App for PlatformApp<Android> {
@@ -93,80 +137,66 @@ impl eframe::App for PlatformApp<Android> {
if _x != self.platform.window_size[0] || _y != self.platform.window_size[1] {
self.platform.window_size[0] = _x;
self.platform.window_size[1] = _y;
self.platform.cutouts = get_display_cutouts(&self.platform.android_app);
self.platform.cutouts = Self::get_display_cutouts(&self.platform.android_app);
}
let is_dark = ctx.style().visuals.dark_mode;
egui::TopBottomPanel::top("top_padding_panel")
.frame(egui::Frame {
shadow: Shadow::NONE,
fill: if is_dark {Color32::BLACK} else {Color32::WHITE},
..Default::default()
})
.show_separator_line(false)
.resizable(false)
.exact_height(self.platform.cutouts[0] as f32)
.show(ctx, |ui| {});
padding_panels(self, ctx);
egui::TopBottomPanel::bottom("bottom_padding_panel")
egui::CentralPanel::default()
.frame(egui::Frame {
shadow: Shadow::NONE,
fill: if is_dark {Color32::BLACK} else {Color32::WHITE},
..Default::default()
inner_margin: egui::style::Margin::same(0.0),
outer_margin: egui::style::Margin::same(0.0),
fill: ctx.style().visuals.panel_fill,
.. Default::default()
})
.show_separator_line(false)
.resizable(false)
.exact_height(self.platform.cutouts[2] as f32)
.show(ctx, |ui| {});
egui::SidePanel::right("right_padding_panel")
.frame(egui::Frame {
shadow: Shadow::NONE,
fill: if is_dark {Color32::BLACK} else {Color32::WHITE},
..Default::default()
})
.show_separator_line(false)
.resizable(false)
.default_width(self.platform.cutouts[1] as f32)
.show(ctx, |ui| {});
egui::SidePanel::left("left_padding_panel")
.frame(egui::Frame {
shadow: Shadow::NONE,
fill: if is_dark {Color32::BLACK} else {Color32::WHITE},
..Default::default()
})
.show_separator_line(false)
.resizable(false)
.default_width(self.platform.cutouts[3] as f32)
.show(ctx, |ui| {});
egui::CentralPanel::default().show(ctx, |ui| {
self.screens.ui(ctx, frame, &self.platform);
});
.show(ctx, |ui| {
self.screens.ui(ui, frame, &self.platform);
});
}
}
fn get_display_cutouts(app: &AndroidApp) -> [i32; 4] {
let vm = unsafe { jni::JavaVM::from_raw(app.vm_as_ptr() as _) }.unwrap();
let mut env = vm.attach_current_thread().unwrap();
let activity = unsafe {
JObject::from_raw(app.activity_as_ptr() as jni::sys::jobject)
};
let _res = env
.call_method(
activity,
"getDisplayCutouts",
"()[I",
&[],
)
.unwrap();
let mut array: [i32; 4] = [0; 4];
let object: jni::sys::jobject = unsafe { _res.as_jni().l };
unsafe {
env.get_int_array_region(JPrimitiveArray::from(
JObject::from_raw(object)), 0, array.as_mut()
).unwrap();
}
array
}
fn padding_panels(app: &PlatformApp<Android>, ctx: &egui::Context) {
egui::TopBottomPanel::top("top_padding_panel")
.frame(egui::Frame {
inner_margin: egui::style::Margin::same(0.0),
fill: ctx.style().visuals.panel_fill,
..Default::default()
})
.show_separator_line(false)
.resizable(false)
.exact_height(app.platform.cutouts[0] as f32)
.show(ctx, |ui| {});
egui::TopBottomPanel::bottom("bottom_padding_panel")
.frame(egui::Frame {
inner_margin: egui::style::Margin::same(0.0),
fill: ctx.style().visuals.panel_fill,
..Default::default()
})
.show_separator_line(false)
.resizable(false)
.exact_height(app.platform.cutouts[2] as f32)
.show(ctx, |ui| {});
egui::SidePanel::right("right_padding_panel")
.frame(egui::Frame {
inner_margin: egui::style::Margin::same(0.0),
fill: ctx.style().visuals.panel_fill,
..Default::default()
})
.show_separator_line(false)
.resizable(false)
.max_width(app.platform.cutouts[1] as f32)
.show(ctx, |ui| {});
egui::SidePanel::left("left_padding_panel")
.frame(egui::Frame {
inner_margin: egui::style::Margin::same(0.0),
fill: ctx.style().visuals.panel_fill,
..Default::default()
})
.show_separator_line(false)
.resizable(false)
.max_width(app.platform.cutouts[3] as f32)
.show(ctx, |ui| {});
}