【问题标题】:Creating a new window in a GNOME extension在 GNOME 扩展中创建新窗口
【发布时间】:2023-02-26 05:07:12
【问题描述】:

tl;博士

如何在 GNOME 扩展中创建一个空窗口?

语境

我正在开发一个扩展程序,它将在一个窗口中管理多个窗口。我想要实现的第一件事就是创建一个新的空窗口。

我试过的

我发现 an example 正是这样做的(但不是在扩展中),就像这样:

import Gtk from "gi://Gtk?version=4.0";
import Adw from "gi://Adw?version=1";
import system from "system";

const application = new Adw.Application({
  application_id: "com.example.Application",
});

application.connect("activate", () => {
  // create a Gtk Window belonging to the application itself
  const window = new Gtk.ApplicationWindow({
    application,
    title: "Welcome to GNOME",
  });
  window.present();
});

/*
* Run the application, exit with the value returned by
* running the program
*/
const exit_code = application.run([system.programInvocationName, ...ARGV]);
system.exit(exit_code);

所以我在我的扩展中尝试了以下内容:

const Gtk = imports.gi.Gtk;
const Adw = imports.gi.Adw;

const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();

const globals = {
    _version: "v4"
}

function enable() {
    log(`enabling ${Me.metadata.name} ${globals._version}`);

    const application = new Adw.Application({
        application_id: "com.example.Application"
    })
    let testWindow = Gtk.ApplicationWindow.new(application)
    testWindow.present_with_time(Date.now())
}

但是在测试时(在嵌套 shell 中),出现以下错误:

(gnome-shell:234231): Gjs-WARNING **: 12:51:46.718: JS ERROR: Extension helloworld@klino.me: Error: Requiring Adw, version none: Requiring namespace 'Gtk' version '4.0', but '3.0' is already loaded

【问题讨论】:

    标签: javascript gnome-shell-extensions gjs


    【解决方案1】:

    GNOME Shell 扩展在 GNOME Shell 的进程中。这意味着,您不能加载与 GNOME Shell 使用的库冲突的库。例如,您不能在同一个进程中加载​​ GTK 3 和 GTK 4,因为它们的 ABI 相互冲突。

    Adwaita 库需要 GTK 4,而 GNOME Shell 进程已经加载了 GTK 3。这就是您看到此错误消息的原因。

    GTK 3 可能只是因为您没有指定imports.gi.Gtk 的版本而被加载。如果是这种情况,请尝试在导入前添加 imports.gi.versions['Gtk'] = '4.0';。

    另一方面,您可能正在为其他目的加载 GTK 3 的旧版本 GNOME Shell 上测试您的扩展。您可以做几件事: 为 GTK 3 调整您的代码;使 Adwaita 成为扩展的可选依赖项,以便仅在可能的情况下加载它,并维护 2 个代码路径;或者声明您的扩展需要足够新的 GNOME Shell 版本,以便您可以使用 GTK 4。(当然,在该版本上进行开发和测试。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多