#gnome #dbus #shell #api #power-management #api-bindings #gsettings

nightly gnome-dbus-api

一个简单与 GNOME DBus 和 Gsettings 交互的 API

11 个版本 (5 个稳定版)

1.0.4 2023年10月14日
1.0.0 2023年10月13日
0.1.6 2023年10月9日

#799GUI

Download history 68/week @ 2024-03-28 37/week @ 2024-04-04

每月 77 次下载

MIT 协议

140KB
885

Rust dbus 和 dconf gnome api 绑定

Crates.io docs.rs Crates.io Crates.io

一个友好的 API,用于与 gnome shell、freedesktop 以及在 Ubuntu gnome 上可用的其他 D-Bus 服务和设置进行交互。

该项目旨在用于 Sittly,一个基于 tauri 的应用程序,你将在示例中看到这一趋势。

免责声明:我不是 Rust 专家,我在学习 Rust,并使用此项目来学习。如果您看到可以改进的地方,请打开一个 issue 或 PR。

用法

系统应用

此应用结构预计将用于 GUI 中显示系统上安装的所有应用。

因此,app.icon 是一个可以编码为 base64 的 png 图像,可以使用 app.get_base64_icon() 在 GUI 中显示。

use gnome_dbus_api::handlers::easy_gnome::apps::Apps;

#[derive(Serialize, Deserialize, Clone)]
struct AppStruct {
    name: String,
    icon: Option<String>,
    description: String,
}
// Init gtk global instance to use gtk related functions
// If you already have a gtk instance running you must skip this step
gtk::init().unwrap();

async fn get_all_apps() -> Result<Vec<AppStruct>, String> {
  let apps_instance = Apps::new();
  let apps = apps_instance.get_apps();
  let apps_struct: Vec<AppStruct> = apps
      .iter()
      .map(|app| {
          let base64 = app.get_base64_icon();
          let app_struct = AppStruct {
              name: app.name.to_string(),
              icon: base64,
              description: match &app.description {
                  Some(description) => description.to_string(),
                  None => String::from(""),
              },
          };
          app_struct
      })
      .collect();
  Ok(apps_struct)
}

屏幕

use gnome_dbus_api::handlers::easy_gnome::screen;

async fn brightness_up() -> Result<(), String> {
    screen::step_up().await;
    Ok(())
}
async fn brightness_down() -> Result<(), String> {
    screen::step_down().await;
    Ok(())
}
async fn get_brightness() -> Result<i32, String> {
    let brightness = screen::brightness().await;
    Ok(brightness)
}
async fn set_brightness(value: i32) -> Result<(), String> {
    screen::set_brightness(value).await;
    Ok(())
}

夜间模式

use gnome_dbus_api::handlers::easy_gnome::nightlight;

fn get_temperature() {
    let temperature: u32 = nightlight::get_temperature();
}
fn set_temperature() {
    let temperature: u32 = 3000;
    nightlight::set_temperature(temperature);
}

fn reset_temperature() {
    nightlight::reset_temperature();
}
fn set_nightlight_active() {
    let active = true;
    nightlight::set_nightlight_active(active);
}
fn get_nightlight_active() {
   nightlight::get_nightlight_active()
}

截图

use gnome_dbus_api::handlers::easy_gnome::screenshot;
async fn pick_color() {
  let (r, g, b) = screenshot::pick_color().await;
}

电源

use gnome_dbus_api::handlers::easy_gnome::power;
async fn power_off() {
power::power_off().await;
}
async fn reboot() {
power::reboot().await;
}
async fn suspend() {
power::suspend().await;
}

外围设备

use gnome_dbus_api::handlers::easy_gnome::peripherals;


fn set_keyboard_press_delay() {
    let delay = 100;
    peripherals::set_keyboard_press_delay(delay).unwrap();
    assert_eq!(peripherals::get_keyboard_press_delay().unwrap(), delay);
}

fn reset_keyboard_press_delay() {
    let default_delay = 500;
    peripherals::reset_keyboard_press_delay().unwrap();
    assert_eq!(
        peripherals::get_keyboard_press_delay().unwrap(),
        default_delay
    );
}

fn set_keyboard_repeat_interval() {
    let interval = 100;
    peripherals::set_keyboard_repeat_interval(interval).unwrap();
    assert_eq!(
        peripherals::get_keyboard_repeat_interval().unwrap(),
        interval
    );
}

fn reset_keyboard_repeat_interval() {
    let default_interval = 30;
    peripherals::reset_keyboard_repeat_interval().unwrap();
    assert_eq!(
        peripherals::get_keyboard_repeat_interval().unwrap(),
        default_interval
    );
}

fn set_mouse_natural_scroll() {
    peripherals::set_mouse_natural_scroll(true).unwrap();
    assert_eq!(peripherals::get_mouse_natural_scroll().unwrap(), true);
}

fn reset_mouse_natural_scroll() {
    peripherals::reset_mouse_natural_scroll().unwrap();
    assert_eq!(peripherals::get_mouse_natural_scroll().unwrap(), false);
}

fn set_touchpad_tap_to_click() {
    peripherals::set_touchpad_tap_to_click(false).unwrap();
    assert_eq!(peripherals::get_touchpad_tap_to_click().unwrap(), false);
}

fn reset_touchpad_tap_to_click() {
    peripherals::reset_touchpad_tap_to_click().unwrap();
    assert_eq!(peripherals::get_touchpad_tap_to_click().unwrap(), false);
}

fn set_two_finger_scroll() {
    peripherals::set_two_finger_scroll(false).unwrap();
    assert_eq!(peripherals::get_two_finger_scroll().unwrap(), false);
}

fn reset_two_finger_scroll() {
    peripherals::reset_two_finger_scroll().unwrap();
    assert_eq!(peripherals::get_two_finger_scroll().unwrap(), true);
}

电池

use gnome_dbus_api::handlers::easy_gnome::battery;

async fn get_battery_display() {
    let battery_display = battery::get_current_device_battery().await.unwrap();

    let full_design_battery = battery_display.energy_full_design().await.unwrap();
    let full_battery = battery_display.energy_full().await.unwrap();
    let current_battery = battery_display.energy().await.unwrap();
    let percentage = battery_display.percentage().await.unwrap();
    let battery_state = battery_display.state().await.unwrap();
    let temperature = battery_display.temperature().await.unwrap();
    let is_rechargable = battery_display.is_rechargeable().await.unwrap();
    let model = battery_display.model().await.unwrap();
    let vendor = battery_display.vendor().await.unwrap();
    let power_supply = battery_display.power_supply().await.unwrap();
    let battery_type = battery_display.type_().await.unwrap();
}

async fn get_devices_battery() {
    let battery_devices = battery::get_devices_battery().await.unwrap();

    for device in battery_devices {
        let full_design_battery = device.energy_full_design().await.unwrap();
        let full_battery = device.energy_full().await.unwrap();
        let current_battery = device.energy().await.unwrap();
        let percentage = device.percentage().await.unwrap();
        let battery_state = device.state().await.unwrap();
        let temperature = device.temperature().await.unwrap();
        let is_rechargable = device.is_rechargeable().await.unwrap();
        let model = device.model().await.unwrap();
        let vendor = device.vendor().await.unwrap();
        let power_supply = device.power_supply().await.unwrap();
        let battery_type = device.type_().await.unwrap();

        println!(
            "full_design_battery: {} full_battery: {} current_battery: {} percentage: {} battery_state: {:?} temperature: {} is_rechargable: {} model: {} vendor: {} power_supply: {} battery_type: {:?}",
            full_design_battery, full_battery, current_battery, percentage, battery_state, temperature, is_rechargable, model, vendor, power_supply,battery_type
        );
    }
}

gnome 扩展

use gnome_dbus_api::handlers::easy_gnome::extensions;

async fn get_extensions() {
    let extensions = extensions::get_extensions().await;
    assert!(extensions.len() > 0);
    println!("{:?}", extensions);
}

async fn launch_extension_preferences() {
    let _extensions_list = extensions::get_extensions().await;
    // You can get the extension uuid from the extensions::get_extensions() function
    let extension_uuid = "[email protected]";
    extensions::open_extension_preferences(extension_uuid).await
}

async fn disable_extension() {
    let _extensions_list = extensions::get_extensions().await;
    // You can get the extension uuid from the extensions::get_extensions() function
    let extension_uuid = "[email protected]";
    extensions::disable_extension(extension_uuid).await
}

async fn enable_extension() {
    let _extensions_list = extensions::get_extensions().await;
    // You can get the extension uuid from the extensions::get_extensions() function
    let extension_uuid = "[email protected]";
    extensions::enable_extension(extension_uuid).await
}

async fn uninstall_extension() {
    let _extensions_list = extensions::get_extensions().await;
    // You can get the extension uuid from the extensions::get_extensions() function
    let extension_uuid = "[email protected]";
    extensions::uninstall_extension(extension_uuid).await
}

功能

  • 电源管理

    • 关机
    • 重启
    • 挂起
  • 区域设置

    • 获取 x11 布局
  • gnome 扩展

    • 获取扩展
    • 启用扩展
    • 禁用扩展
    • 卸载扩展
  • gnome shell 截图

    • 选择颜色
  • 设置

    • 夜间模式
      • 获取夜间模式状态
      • 设置夜间模式状态
      • 获取夜间模式温度
      • 设置夜间模式温度
  • Gsettings Dconf (https://crates.io/crates/dconf_rs/0.3.0)

    • image
    • org.gnome.desktop.peripherals.touchpad two-finger-scrolling-enabled true
    • org.gnome.desktop.peripherals.touchpad tap-to-click true
    • org.gnome.desktop.peripherals.mouse natural-scroll true
    • org.gnome.desktop.peripherals.keyboard repeat-interval 30 (初始按键重复延迟)
    • org.gnome.desktop.peripherals.keyboard delay 500 (初始按键重复延迟)
    • org.gnome.desktop.interface show-battery-percentage true
    • org.gnome.desktop.interface locate-pointer false (与 Ctrl 键一起使用)
    • org.gnome.desktop.interface cursor-size 24

依赖关系

~50MB
~738K SLoC