feat: Change App Name, add rust/set_foreground_window

This commit is contained in:
2024-04-28 21:50:36 +08:00
parent e9c494096f
commit c5549cf4c8
9 changed files with 160 additions and 39 deletions

View File

@ -22,4 +22,5 @@ anyhow = "1.0"
win32job = "2"
lazy_static = "1.4"
scopeguard = "1.2"
notify-rust = "4"
notify-rust = "4"
windows = { version = "0.56.0", features = ["Win32_System_Services", "Win32_UI_WindowsAndMessaging"] }

View File

@ -1,7 +1,7 @@
use std::collections::HashMap;
use std::sync::Arc;
use async_std::task::block_on;
use async_std::task::block_on;
use lazy_static::lazy_static;
use scopeguard::defer;
use tokio::io::AsyncBufReadExt;
@ -9,6 +9,9 @@ use tokio::io::AsyncWriteExt;
use tokio::io::BufReader;
use tokio::process::ChildStdin;
use tokio::sync::Mutex;
use windows::core::{HSTRING, PCWSTR};
use windows::Win32::Foundation::HWND;
use windows::Win32::UI::WindowsAndMessaging;
use crate::frb_generated::StreamSink;
@ -35,9 +38,9 @@ lazy_static! {
}
pub async fn start(
executable: String,
executable: &str,
arguments: Vec<String>,
working_directory: String,
working_directory: &str,
stream_sink: StreamSink<RsProcessStreamData>,
) {
let stream_sink_arc = Arc::from(stream_sink);
@ -161,3 +164,17 @@ async fn _process_output<R>(
stream_sink.add(message).unwrap();
}
}
pub fn set_foreground_window(window_name: &str) -> anyhow::Result<bool> {
let window_name_p: PCWSTR = PCWSTR(HSTRING::from(window_name).as_ptr());
let h = unsafe { WindowsAndMessaging::FindWindowW(PCWSTR::null(), window_name_p) };
if h == HWND::default() {
return Ok(false);
}
let sr = unsafe { WindowsAndMessaging::ShowWindow(h, WindowsAndMessaging::SW_RESTORE) };
if !sr.as_bool() {
return Ok(false);
}
let r = unsafe { WindowsAndMessaging::SetForegroundWindow(h) };
Ok(r.as_bool())
}

View File

@ -215,6 +215,14 @@ pub extern "C" fn frbgen_starcitizen_doctor_wire_send_notify(
wire_send_notify_impl(port_, summary, body, app_name, app_id)
}
#[no_mangle]
pub extern "C" fn frbgen_starcitizen_doctor_wire_set_foreground_window(
port_: i64,
window_name: *mut wire_cst_list_prim_u_8_strict,
) {
wire_set_foreground_window_impl(port_, window_name)
}
#[no_mangle]
pub extern "C" fn frbgen_starcitizen_doctor_wire_start(
port_: i64,

View File

@ -31,7 +31,7 @@ flutter_rust_bridge::frb_generated_boilerplate!(
default_rust_auto_opaque = RustAutoOpaqueNom,
);
pub(crate) const FLUTTER_RUST_BRIDGE_CODEGEN_VERSION: &str = "2.0.0-dev.32";
pub(crate) const FLUTTER_RUST_BRIDGE_CODEGEN_CONTENT_HASH: i32 = 1067953400;
pub(crate) const FLUTTER_RUST_BRIDGE_CODEGEN_CONTENT_HASH: i32 = -1186168522;
// Section: executor
@ -170,6 +170,26 @@ fn wire_send_notify_impl(
},
)
}
fn wire_set_foreground_window_impl(
port_: flutter_rust_bridge::for_generated::MessagePort,
window_name: impl CstDecode<String>,
) {
FLUTTER_RUST_BRIDGE_HANDLER.wrap_normal::<flutter_rust_bridge::for_generated::DcoCodec, _, _>(
flutter_rust_bridge::for_generated::TaskInfo {
debug_name: "set_foreground_window",
port: Some(port_),
mode: flutter_rust_bridge::for_generated::FfiCallMode::Normal,
},
move || {
let api_window_name = window_name.cst_decode();
move |context| {
transform_result_dco((move || {
crate::api::rs_process::set_foreground_window(&api_window_name)
})())
}
},
)
}
fn wire_start_impl(
port_: flutter_rust_bridge::for_generated::MessagePort,
executable: impl CstDecode<String>,
@ -198,9 +218,9 @@ fn wire_start_impl(
(move || async move {
Result::<_, ()>::Ok(
crate::api::rs_process::start(
api_executable,
&api_executable,
api_arguments,
api_working_directory,
&api_working_directory,
api_stream_sink,
)
.await,
@ -242,6 +262,12 @@ fn wire_write_impl(
// Section: dart2rust
impl CstDecode<bool> for bool {
// Codec=Cst (C-struct based), see doc to use other codecs
fn cst_decode(self) -> bool {
self
}
}
impl CstDecode<i32> for i32 {
// Codec=Cst (C-struct based), see doc to use other codecs
fn cst_decode(self) -> i32 {
@ -350,6 +376,13 @@ impl SseDecode for String {
}
}
impl SseDecode for bool {
// Codec=Sse (Serialization based), see doc to use other codecs
fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self {
deserializer.cursor.read_u8().unwrap() != 0
}
}
impl SseDecode for i32 {
// Codec=Sse (Serialization based), see doc to use other codecs
fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self {
@ -566,13 +599,6 @@ impl SseDecode for () {
fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self {}
}
impl SseDecode for bool {
// Codec=Sse (Serialization based), see doc to use other codecs
fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self {
deserializer.cursor.read_u8().unwrap() != 0
}
}
fn pde_ffi_dispatcher_primary_impl(
func_id: i32,
port: flutter_rust_bridge::for_generated::MessagePort,
@ -754,6 +780,13 @@ impl SseEncode for String {
}
}
impl SseEncode for bool {
// Codec=Sse (Serialization based), see doc to use other codecs
fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) {
serializer.cursor.write_u8(self as _).unwrap();
}
}
impl SseEncode for i32 {
// Codec=Sse (Serialization based), see doc to use other codecs
fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) {
@ -954,13 +987,6 @@ impl SseEncode for () {
fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) {}
}
impl SseEncode for bool {
// Codec=Sse (Serialization based), see doc to use other codecs
fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) {
serializer.cursor.write_u8(self as _).unwrap();
}
}
#[cfg(not(target_family = "wasm"))]
#[path = "frb_generated.io.rs"]
mod io;