Flutter桌面入口管理插件freedesktop_desktop_entry的使用

发布于 1周前 作者 sinazl 来自 Flutter

Flutter桌面入口管理插件freedesktop_desktop_entry的使用

这是一个用于在Linux上解析freedesktop(XDG)桌面入口的Dart包。

特性

  • 可以通过键获取值,包括动作组。
  • 可以获取本地化值。
  • 可以查找图标。

此包提供了DesktopEntryKey枚举类以方便使用,但它不对值类型做出任何假设,也不判断键是否为必需。所有键都被视为可选。

使用方法

解析一个桌面入口文件

import 'package:freedesktop_desktop_entry/freedesktop_desktop_entry.dart';
import 'dart:io';

final file = File("desktop-entry.desktop");
DesktopEntry desktopEntry = await DesktopEntry.parseFile(file);

本地化整个桌面入口

LocalizedDesktopEntry localizedDesktopEntry = desktopEntry.localize(lang: 'fr', country: 'BE');

获取本地化值

String? localizedComment = localizedDesktopEntry.entries[DesktopEntryKey.comment.string];
// 或者
String? localizedComment = desktopEntry.entries[DesktopEntryKey.comment.string]?.localize(lang: 'fr', country: 'BE');

除非你只对几个字段感兴趣,否则建议对整个桌面入口进行本地化,以避免每次都要指定语言。

localize 方法会根据官方的语言匹配规则进行本地化,并使用默认值如果找不到匹配的语言。这可能是你想要做的。

获取默认值

String? name = desktopEntry.entries[DesktopEntryKey.name.string]?.value;
bool? terminal = desktopEntry.entries[DesktopEntryKey.terminal.string]?.value.getBoolean();
List<String>? keywords = desktopEntry.entries[DesktopEntryKey.keywords.string]?.value.getStringList();
bool? startupNotify = desktopEntry.entries['X-KDE-StartupNotify']?.value.getBoolean();

按精确语言获取值

String? frenchComment = desktopEntry.entries[DesktopEntryKey.comment.string]?.localizedValues[Locale(lang: 'fr', country: 'BE')];

查找图标

final themes = FreedesktopIconThemes();
File? file = await themes.findIcon(
    IconQuery(
        name: 'firefox',
        size: 32,
        scale: 2,
        extensions: ['png'],
    ),
);

第一次执行图标查找时,系统中安装的所有主题都将被索引。如果你希望预加载主题,可以调用FreedesktopIconThemes.loadThemes

如果新图标已安装,下次调用FreedesktopIconThemes.findIcon时,它将再次索引所有主题,然后返回图标。

你可以在IconQuery中指定preferredThemes。这将优先搜索你给定的主题名称列表,然后再在其他地方搜索。这些主题名称必须是目录名,而不是用户可见的名称。例如 breeze-dark 而不是 Breeze Dark

示例代码

以下是一个完整的示例代码:

import 'dart:io';

import 'package:freedesktop_desktop_entry/freedesktop_desktop_entry.dart';

void main() async {
  Directory.current = "test/desktop_entry_files/";
  final file = File("desktop-entry-1.desktop");

  DesktopEntry desktopEntry = await DesktopEntry.parseFile(file);

  LocalizedDesktopEntry localizedDesktopEntry = desktopEntry.localize(lang: 'fr', country: 'BE');
  String? frenchName = localizedDesktopEntry.entries[DesktopEntryKey.name.string];
  print(frenchName);

  String? defaultName = desktopEntry.entries[DesktopEntryKey.name.string]?.value;
  print(defaultName);

  List<String>? frenchKeywords = localizedDesktopEntry.entries[DesktopEntryKey.keywords.string]?.getStringList();
  print(frenchKeywords);

  List<String>? englishKeywords =
      desktopEntry.entries[DesktopEntryKey.keywords.string]?.localizedValues[Locale(lang: 'en')]?.getStringList();
  print(englishKeywords);

  print(desktopEntry.id);

  final allFiles = await parseAllInstalledDesktopFiles();
  print(allFiles.length);
}

更多关于Flutter桌面入口管理插件freedesktop_desktop_entry的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter桌面入口管理插件freedesktop_desktop_entry的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,下面是一个关于如何使用 freedesktop_desktop_entry 插件在 Flutter 桌面应用中管理入口点的示例代码。这个插件允许你创建和管理 .desktop 文件,这对于 Linux 桌面环境中的应用程序启动非常重要。

首先,确保你已经在 pubspec.yaml 文件中添加了 freedesktop_desktop_entry 依赖:

dependencies:
  flutter:
    sdk: flutter
  freedesktop_desktop_entry: ^最新版本号  # 请替换为实际的最新版本号

然后运行 flutter pub get 来获取依赖。

接下来,你可以在 Flutter 应用中使用这个插件来管理 .desktop 文件。以下是一个简单的示例,展示如何创建和更新一个 .desktop 文件。

import 'package:flutter/material.dart';
import 'package:freedesktop_desktop_entry/freedesktop_desktop_entry.dart';
import 'dart:io';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _desktopEntryPath = '';

  @override
  void initState() {
    super.initState();
    _createDesktopEntry();
  }

  Future<void> _createDesktopEntry() async {
    final desktopEntry = DesktopEntryBuilder()
      ..setName('MyFlutterApp')
      ..setExec('flutter run --machine --target lib/main.dart -d linux')
      ..setComment('My Flutter Application for Linux')
      ..setIcon('path/to/icon');  // 替换为实际的图标路径

    String desktopEntryContent = desktopEntry.build();
    
    // 写入到用户桌面目录中的一个 .desktop 文件
    Directory desktopDir = Directory(Platform.environment['XDG_CURRENT_DESKTOP'] == 'GNOME' 
        ? '${Platform.environment['HOME']}/Desktop' 
        : '/usr/share/applications');
    File desktopEntryFile = File('${desktopDir.path}/myflutterapp.desktop');

    await desktopEntryFile.writeAsString(desktopEntryContent);
    
    setState(() {
      _desktopEntryPath = desktopEntryFile.path;
    });

    print('Desktop entry created at: $_desktopEntryPath');
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Flutter Desktop Entry Management'),
        ),
        body: Center(
          child: Text('Desktop Entry Path: $_desktopEntryPath\n'
              'Check your desktop for the MyFlutterApp launcher.'),
        ),
      ),
    );
  }
}

注意

  1. Exec 路径:在 .desktop 文件中,Exec 字段指定了启动应用程序的命令。上面的示例中使用了 flutter run 命令,这仅用于开发阶段。在生产环境中,你需要打包你的 Flutter 应用并指向可执行文件,例如 ./myflutterapp

  2. 图标路径:确保提供的图标路径是正确的,并且图标文件存在。

  3. 桌面目录:示例中根据 GNOME 桌面环境判断了桌面目录,你可能需要根据不同的桌面环境调整这部分代码。

  4. 权限:写入 /usr/share/applications 可能需要管理员权限,如果你希望将 .desktop 文件安装到系统级目录,请确保你的应用有适当的权限。

  5. 桌面环境变量Platform.environment['XDG_CURRENT_DESKTOP'] 用于检测当前的桌面环境,但并非所有环境都设置了这个变量。你可能需要额外的逻辑来处理不同的桌面环境。

这个示例提供了一个基本的框架,你可以根据需要进行扩展和修改,以适应你的具体需求。

回到顶部