Skip to content

Commit

Permalink
Merge pull request #11 from guchengxi1994/master
Browse files Browse the repository at this point in the history
fix linux
  • Loading branch information
guchengxi1994 authored Apr 17, 2024
2 parents f7e2cf9 + 30b06bf commit bb06544
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 23 deletions.
2 changes: 1 addition & 1 deletion lib/software_monitor/notifier/monitor_item_notifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class MonitorItemNotifier extends AutoDisposeAsyncNotifier<MonitorItemState> {

final defaultCatalog =
await database.isar!.softwareCatalogs.where().findFirst();
final firstTimeSoftwares = await smapi.getWindowsInstalledSoftwares();
final firstTimeSoftwares = await smapi.getInstalledSoftwares();
List<Software> softwares = [];
for (final i in firstTimeSoftwares) {
Software software = Software()..name = i.name;
Expand Down
4 changes: 2 additions & 2 deletions lib/src/rust/api/software_monitor_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import '../frb_generated.dart';
import '../software_monitor/software.dart';
import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart';

Future<List<Software>> getWindowsInstalledSoftwares({dynamic hint}) =>
RustLib.instance.api.getWindowsInstalledSoftwares(hint: hint);
Future<List<Software>> getInstalledSoftwares({dynamic hint}) =>
RustLib.instance.api.getInstalledSoftwares(hint: hint);

Stream<Int64List> softwareWatchingMessageStream({dynamic hint}) =>
RustLib.instance.api.softwareWatchingMessageStream(hint: hint);
Expand Down
11 changes: 5 additions & 6 deletions lib/src/rust/frb_generated.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ abstract class RustLibApi extends BaseApi {
Future<void> addToWatchingList(
{required int id, required String name, dynamic hint});

Future<List<Software>> getWindowsInstalledSoftwares({dynamic hint});
Future<List<Software>> getInstalledSoftwares({dynamic hint});

Future<void> initMonitor({required List<(int, String)> items, dynamic hint});

Expand Down Expand Up @@ -190,7 +190,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
);

@override
Future<List<Software>> getWindowsInstalledSoftwares({dynamic hint}) {
Future<List<Software>> getInstalledSoftwares({dynamic hint}) {
return handler.executeNormal(NormalTask(
callFfi: (port_) {
final serializer = SseSerializer(generalizedFrbRustBinding);
Expand All @@ -201,16 +201,15 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
decodeSuccessData: sse_decode_list_software,
decodeErrorData: null,
),
constMeta: kGetWindowsInstalledSoftwaresConstMeta,
constMeta: kGetInstalledSoftwaresConstMeta,
argValues: [],
apiImpl: this,
hint: hint,
));
}

TaskConstMeta get kGetWindowsInstalledSoftwaresConstMeta =>
const TaskConstMeta(
debugName: "get_windows_installed_softwares",
TaskConstMeta get kGetInstalledSoftwaresConstMeta => const TaskConstMeta(
debugName: "get_installed_softwares",
argNames: [],
);

Expand Down
9 changes: 7 additions & 2 deletions rust/src/api/software_monitor_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ use crate::software_monitor::{
monitor::{start_watch, WATCHING_LIST, WATCHING_MESSAGE_SINK},
software,
};
#[cfg(target_os = "windows")]
use winreg::enums::{HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE};

#[cfg(target_os = "windows")]
pub fn get_windows_installed_softwares() -> Vec<software::Software> {
pub fn get_installed_softwares() -> Vec<software::Software> {
let local = software::_Hkey(HKEY_LOCAL_MACHINE);
let user = software::_Hkey(HKEY_CURRENT_USER);

Expand All @@ -20,6 +21,11 @@ pub fn get_windows_installed_softwares() -> Vec<software::Software> {
v1
}

#[cfg(target_os = "linux")]
pub fn get_installed_softwares() -> Vec<software::Software> {
vec![]
}

#[flutter_rust_bridge::frb(sync)]
pub fn software_watching_message_stream(s: StreamSink<Vec<i64>>) -> anyhow::Result<()> {
let mut stream = WATCHING_MESSAGE_SINK.write().unwrap();
Expand All @@ -28,7 +34,6 @@ pub fn software_watching_message_stream(s: StreamSink<Vec<i64>>) -> anyhow::Resu
}

#[flutter_rust_bridge::frb(sync)]
#[cfg(target_os = "windows")]
pub fn software_watching_with_foreground_message_stream(
s: StreamSink<(Vec<i64>, String)>,
) -> anyhow::Result<()> {
Expand Down
10 changes: 4 additions & 6 deletions rust/src/frb_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,15 @@ fn wire_add_to_watching_list_impl(
},
)
}
fn wire_get_windows_installed_softwares_impl(
fn wire_get_installed_softwares_impl(
port_: flutter_rust_bridge::for_generated::MessagePort,
ptr_: flutter_rust_bridge::for_generated::PlatformGeneralizedUint8ListPtr,
rust_vec_len_: i32,
data_len_: i32,
) {
FLUTTER_RUST_BRIDGE_HANDLER.wrap_normal::<flutter_rust_bridge::for_generated::SseCodec, _, _>(
flutter_rust_bridge::for_generated::TaskInfo {
debug_name: "get_windows_installed_softwares",
debug_name: "get_installed_softwares",
port: Some(port_),
mode: flutter_rust_bridge::for_generated::FfiCallMode::Normal,
},
Expand All @@ -159,9 +159,7 @@ fn wire_get_windows_installed_softwares_impl(
deserializer.end();
move |context| {
transform_result_sse((move || {
Result::<_, ()>::Ok(
crate::api::software_monitor_api::get_windows_installed_softwares(),
)
Result::<_, ()>::Ok(crate::api::software_monitor_api::get_installed_softwares())
})())
}
},
Expand Down Expand Up @@ -784,7 +782,7 @@ fn pde_ffi_dispatcher_primary_impl(
match func_id {
2 => wire_init_app_impl(port, ptr, rust_vec_len, data_len),
6 => wire_add_to_watching_list_impl(port, ptr, rust_vec_len, data_len),
3 => wire_get_windows_installed_softwares_impl(port, ptr, rust_vec_len, data_len),
3 => wire_get_installed_softwares_impl(port, ptr, rust_vec_len, data_len),
8 => wire_init_monitor_impl(port, ptr, rust_vec_len, data_len),
7 => wire_remove_from_watching_list_impl(port, ptr, rust_vec_len, data_len),
9 => wire_create_event_loop_impl(port, ptr, rust_vec_len, data_len),
Expand Down
5 changes: 5 additions & 0 deletions rust/src/software_monitor/foreground_monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::frb_generated::StreamSink;
use cron_job::CronJob;
use std::{collections::HashSet, sync::RwLock};
use sysinfo::System;
#[cfg(target_os = "windows")]
use winapi::um::winuser::{GetForegroundWindow, GetWindowThreadProcessId};

use super::monitor::WATCHING_LIST;
Expand Down Expand Up @@ -55,6 +56,7 @@ pub fn start_monitor_with_foreground() {
cron.start();
}

#[cfg(target_os = "windows")]
fn get_foreground_pid() -> u32 {
let hwnd = unsafe { GetForegroundWindow() };
if hwnd.is_null() {
Expand All @@ -67,3 +69,6 @@ fn get_foreground_pid() -> u32 {

return process_id;
}

#[cfg(target_os = "linux")]
pub fn start_monitor_with_foreground() {}
1 change: 1 addition & 0 deletions rust/src/software_monitor/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

pub mod foreground_monitor;
#[allow(dead_code)]
pub mod monitor;
Expand Down
1 change: 1 addition & 0 deletions rust/src/software_monitor/software.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{collections::HashSet, hash::Hash, io::Error};

use flutter_rust_bridge::frb;
#[cfg(target_os = "windows")]
use winreg::RegKey;

#[derive(Debug)]
Expand Down
2 changes: 0 additions & 2 deletions rust/src/system_monitor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ mod tests;
pub static SYS_MONITOR_MESSAGE_SINK: RwLock<Option<StreamSink<MonitorInfo>>> = RwLock::new(None);

pub struct MonitorInfo {
#[cfg(windows)]
pub disks: Option<Vec<MountedInfo>>,
pub memory: Option<MemoryInfo>,
pub cpu: Option<CpuInfo>,
Expand All @@ -31,7 +30,6 @@ impl MonitorInfo {
}
}

#[cfg(windows)]
pub struct MountedInfo {
pub disk: String,
pub name: String,
Expand Down
12 changes: 8 additions & 4 deletions rust/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
#[allow(unused_braces, dead_code, non_snake_case, unused_variables)]
#[cfg(target_os = "windows")]
mod foreground_test;

#[cfg(target_os = "windows")]
use std::{ffi::OsStr, io::Error, iter::once, os::windows::ffi::OsStrExt};

use flutter_rust_bridge::frb;
#[cfg(target_os = "windows")]
use winreg::{
enums::{HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE},
RegKey,
};

#[cfg(target_os = "windows")]
extern crate systemicons;

#[test]
#[frb(ignore)]
#[cfg(windows)]
#[cfg(target_os = "windows")]
fn test_win_install_list() {
// 打开 Uninstall 键
let hkcu_uninstall =
Expand Down Expand Up @@ -43,7 +47,7 @@ fn test_win_install_list() {

#[test]
#[frb(ignore)]
#[cfg(windows)]
#[cfg(target_os = "windows")]
fn test_win_exe_to_ico() -> anyhow::Result<()> {
use std::{fs::File, io::Write};

Expand All @@ -66,9 +70,9 @@ fn test_win_exe_to_ico() -> anyhow::Result<()> {

#[test]
#[frb(ignore)]
#[cfg(windows)]
#[cfg(target_os = "windows")]
fn test_win_get_all_softwares() -> anyhow::Result<()> {
let r = crate::api::software_monitor_api::get_windows_installed_softwares();
let r = crate::api::software_monitor_api::get_installed_softwares();
for i in &r {
println!("name {:?}", i.name);
println!("path {:?}", i.icon_path);
Expand Down

0 comments on commit bb06544

Please sign in to comment.