platform: passed data at lib, desktop user attention, check existing file on share at android

This commit is contained in:
ardocrat
2024-09-12 21:27:37 +03:00
parent dd45f7ce38
commit d78ec570b0
6 changed files with 172 additions and 134 deletions
+6 -14
View File
@@ -42,8 +42,6 @@ pub struct App<Platform> {
/// Flag to check if it's first draw.
first_draw: bool,
/// Flag to check if attention required after window focus.
attention_required: bool,
}
impl<Platform: PlatformCallbacks> App<Platform> {
@@ -52,14 +50,13 @@ impl<Platform: PlatformCallbacks> App<Platform> {
platform,
content: Content::default(),
resize_direction: None,
first_draw: true,
attention_required: false,
first_draw: true
}
}
/// Draw application content.
pub fn ui(&mut self, ctx: &Context) {
// Set Desktop platform context on first draw.
// Set platform context on first draw.
if self.first_draw {
if View::is_desktop() {
self.platform.set_context(ctx);
@@ -113,22 +110,17 @@ impl<Platform: PlatformCallbacks> App<Platform> {
}
// Provide incoming data to wallets.
if let Some(data) = self.platform.consume_data() {
if let Some(data) = crate::consume_passed_data() {
if !data.is_empty() {
self.content.wallets.on_data(ui, Some(data), &self.platform);
}
self.attention_required = true;
}
});
// Check if desktop window was focused after requested attention.
if self.attention_required && View::is_desktop()
&& ctx.input(|i| i.viewport().focused.unwrap_or(true)) {
self.attention_required = false;
ctx.send_viewport_cmd(
ViewportCommand::RequestUserAttention(egui::UserAttentionType::Reset)
);
ctx.send_viewport_cmd(ViewportCommand::WindowLevel(egui::WindowLevel::Normal));
if self.platform.user_attention_required() &&
ctx.input(|i| i.viewport().focused.unwrap_or(true)) {
self.platform.clear_user_attention();
}
}
+22 -5
View File
@@ -30,7 +30,11 @@ use crate::gui::platform::PlatformCallbacks;
/// Android platform implementation.
#[derive(Clone)]
pub struct Android {
/// Android related state.
android_app: AndroidApp,
/// Context to repaint content and handle viewport commands.
ctx: Arc<RwLock<Option<egui::Context>>>,
}
impl Android {
@@ -38,6 +42,7 @@ impl Android {
pub fn new(app: AndroidApp) -> Self {
Self {
android_app: app,
ctx: Arc::new(RwLock::new(None)),
}
}
@@ -56,6 +61,11 @@ impl Android {
}
impl PlatformCallbacks for Android {
fn set_context(&mut self, ctx: &egui::Context) {
let mut w_ctx = self.ctx.write();
*w_ctx = Some(ctx.clone());
}
fn show_keyboard(&self) {
// Disable NDK soft input show call before fix for egui.
// self.android_app.show_soft_input(false);
@@ -131,9 +141,12 @@ impl PlatformCallbacks for Android {
cache.push("images");
std::fs::create_dir_all(cache.to_str().unwrap())?;
cache.push(name);
let mut image = File::create_new(cache.clone()).unwrap();
image.write_all(data.as_slice()).unwrap();
image.sync_all().unwrap();
if cache.exists() {
std::fs::remove_file(cache.clone())?;
}
let mut image = File::create_new(cache.clone())?;
image.write_all(data.as_slice())?;
image.sync_all()?;
// Call share modal at system.
let vm = unsafe { jni::JavaVM::from_raw(self.android_app.vm_as_ptr() as _) }.unwrap();
let env = vm.attach_current_thread().unwrap();
@@ -168,9 +181,13 @@ impl PlatformCallbacks for Android {
None
}
fn consume_data(&mut self) -> Option<String> {
None
fn request_user_attention(&self) {}
fn user_attention_required(&self) -> bool {
false
}
fn clear_user_attention(&self) {}
}
lazy_static! {
+34 -37
View File
@@ -27,10 +27,14 @@ use crate::gui::platform::PlatformCallbacks;
/// Desktop platform related actions.
#[derive(Clone)]
pub struct Desktop {
/// Flag to check if camera stop is needed.
stop_camera: Arc<AtomicBool>,
/// Context to repaint content and handle viewport commands.
ctx: Arc<RwLock<Option<egui::Context>>>,
/// Flag to check if camera stop is needed.
stop_camera: Arc<AtomicBool>,
/// Flag to check if attention required after window focusing.
attention_required: Arc<AtomicBool>,
}
impl PlatformCallbacks for Desktop {
@@ -120,38 +124,7 @@ impl PlatformCallbacks for Desktop {
None
}
fn consume_data(&mut self) -> Option<String> {
let has_data = {
let r_data = PASSED_DATA.read();
r_data.is_some()
};
if has_data {
// Clear data.
let mut w_data = PASSED_DATA.write();
let data = w_data.clone();
*w_data = None;
return data;
}
None
}
}
impl Desktop {
/// Create new instance with provided extra data from app opening.
pub fn new(data: Option<String>) -> Self {
let mut w_data = PASSED_DATA.write();
*w_data = data;
Self {
stop_camera: Arc::new(AtomicBool::new(false)),
ctx: Arc::new(RwLock::new(None)),
}
}
/// Handle data passed to application.
pub fn on_data(&self, data: String) {
let mut w_data = PASSED_DATA.write();
*w_data = Some(data);
fn request_user_attention(&self) {
let r_ctx = self.ctx.read();
if r_ctx.is_some() {
let ctx = r_ctx.as_ref().unwrap();
@@ -170,6 +143,33 @@ impl Desktop {
}
ctx.request_repaint();
}
self.attention_required.store(true, Ordering::Relaxed);
}
fn user_attention_required(&self) -> bool {
self.attention_required.load(Ordering::Relaxed)
}
fn clear_user_attention(&self) {
let r_ctx = self.ctx.read();
if r_ctx.is_some() {
let ctx = r_ctx.as_ref().unwrap();
ctx.send_viewport_cmd(
ViewportCommand::RequestUserAttention(UserAttentionType::Reset)
);
ctx.send_viewport_cmd(ViewportCommand::WindowLevel(WindowLevel::Normal));
}
self.attention_required.store(false, Ordering::Relaxed);
}
}
impl Desktop {
pub fn new() -> Self {
Self {
stop_camera: Arc::new(AtomicBool::new(false)),
ctx: Arc::new(RwLock::new(None)),
attention_required: Arc::new(AtomicBool::new(false)),
}
}
#[allow(dead_code)]
@@ -255,7 +255,4 @@ impl Desktop {
lazy_static! {
/// Last captured image from started camera.
static ref LAST_CAMERA_IMAGE: Arc<RwLock<Option<(Vec<u8>, u32)>>> = Arc::new(RwLock::new(None));
/// Data passed from deeplink or opened file.
static ref PASSED_DATA: Arc<RwLock<Option<String>>> = Arc::new(RwLock::new(None));
}
+3 -1
View File
@@ -35,5 +35,7 @@ pub trait PlatformCallbacks {
fn share_data(&self, name: String, data: Vec<u8>) -> Result<(), std::io::Error>;
fn pick_file(&self) -> Option<String>;
fn picked_file(&self) -> Option<String>;
fn consume_data(&mut self) -> Option<String>;
fn request_user_attention(&self);
fn user_attention_required(&self) -> bool;
fn clear_user_attention(&self);
}