编程开源技术交流,分享技术与知识

网站首页 > 开源技术 正文

Gnome之爱,少年老成的vala与界面百搭gtk的片段

wxchong 2024-09-03 02:08:22 开源技术 9 ℃ 0 评论

应该vala是新的语言,保留了C的指针和特性,还有C++的易用性,又有许多C#的味道,但总感觉虽少年但却老成,不同于Python和C#这样的生产力工具,又不同于底层开发语言。纯本地应用办公娱乐商务等等今天已经很成熟了,基于浏览器的跨空间应用也是渐渐羽翼丰满,vala未来要飞向哪里就不知道了,但它搭上gtk比其它语言搭上mono要轻量多了。分解一段vala程序。

程序界面

代码框架

using Gtk;

int main (string[] args) {
......
......
return 0;
}

加入gtk的init与main

using Gtk;

int main (string[] args) {
Gtk.init (ref args);
......
......
Gtk.main ();
return 0;
}

建主窗体

using Gtk;

int main (string[] args) {
    Gtk.init (ref args);
    var window = new Window ();
    window.title = "Demo GTK+ Program";
    window.border_width = 10;
    window.window_position = WindowPosition.CENTER;
    window.set_default_size (450, 220);
    window.destroy.connect (Gtk.main_quit);
......
......
Gtk.main ();
return 0;
}

给主窗体加上header_bar

using Gtk;

int main (string[] args) {
    Gtk.init (ref args);
    var window = new Window ();
    window.title = "Demo GTK+ Program";
    window.border_width = 10;
    window.window_position = WindowPosition.CENTER;
    window.set_default_size (450, 220);
    window.destroy.connect (Gtk.main_quit);
  
    var hbar = new HeaderBar();
    hbar.show_close_button = true;
    hbar.title = "Hello TitleBar";
    window.set_titlebar(hbar);
......
......
Gtk.main ();
return 0;
}

试着给窗体加上图标

using Gtk;

int main (string[] args) {
    Gtk.init (ref args);
    var window = new Window ();
    window.title = "Demo GTK+ Program";
    window.border_width = 10;
    window.window_position = WindowPosition.CENTER;
    window.set_default_size (450, 220);
    window.destroy.connect (Gtk.main_quit);

    var hbar = new HeaderBar();
    hbar.show_close_button = true;
    hbar.title = "Hello TitleBar";
    window.set_titlebar(hbar);

    try {
        // Either directly from a file ...
        window.icon = new Gdk.Pixbuf.from_file ("valagtk.png");
        // ... or from the theme
        //window.icon = IconTheme.get_default ().load_icon ("gtk3vala1", 48, 0);
    } catch (Error e) {
        stderr.printf ("Could not load application icon: %s\n", e.message);
    }
......
......
Gtk.main ();
return 0;
}

创建box布局,label和两个button

......
......
    var box = new Box(Orientation.VERTICAL, 4);
    box.homogeneous = true;

    var label = new Label("A label");
    var button = new Button.with_label ("Click me!");
    var button1 = new Button.with_label ("Quit");
......
......

接着写button1和button2执行代码

  ......
  ......
		button.clicked.connect (() => {
        if (button.label == "Click me!") {
           label.label = "Thank you";
           button.label = "Thank you";}
        else {
           label.label = "Click me!";
           button.label = "Click me!";}
    });
    button1.clicked.connect (() => {
        Gtk.main_quit();
    });
......
......

把布局放到窗体上,把label和button放到布局里

......
......
	  window.add (box);
    box.add (label); box.add (button); box.add (button1);
    window.show_all ();
......
......

全部完整的代码

using Gtk;

int main (string[] args) {
    Gtk.init (ref args);
    var window = new Window ();
    window.title = "Demo GTK+ Program";
    window.border_width = 10;
    window.window_position = WindowPosition.CENTER;
    window.set_default_size (450, 220);
    window.destroy.connect (Gtk.main_quit);

    var hbar = new HeaderBar();
    hbar.show_close_button = true;
    hbar.title = "Hello TitleBar";
    window.set_titlebar(hbar);

    try {
        // Either directly from a file ...
        window.icon = new Gdk.Pixbuf.from_file ("valagtk.png");
        // ... or from the theme
        //window.icon = IconTheme.get_default ().load_icon ("gtk3vala1", 48, 0);
    } catch (Error e) {
        stderr.printf ("Could not load application icon: %s\n", e.message);
    }

    var box = new Box(Orientation.VERTICAL, 4);
    box.homogeneous = true;

    var label = new Label("A label");
    var button = new Button.with_label ("Click me!");
    var button1 = new Button.with_label ("Quit");

    button.clicked.connect (() => {
        if (button.label == "Click me!") {
           label.label = "Thank you";
           button.label = "Thank you";}
        else {
           label.label = "Click me!";
           button.label = "Click me!";}
    });
    button1.clicked.connect (() => {
        Gtk.main_quit();
    });

    window.add (box);
    box.add (label); box.add (button); box.add (button1);
    window.show_all ();

    Gtk.main ();
    return 0;
}


ATOM配置

将.atom-build.yml 放置于代码文件夹下即可,内容如下表中的代码。

一直用ATOM,感觉比vs code轻量易用,听说这东西被ms给收购了,真的吗?

cmd: "valac --pkg gtk+-3.0 --pkg gmodule-2.0"
name: "vala Compiler"
args:
  - "{FILE_ACTIVE}"
  - "-o {FILE_ACTIVE_PATH}/{FILE_ACTIVE_NAME_BASE} &&"
  - "{FILE_ACTIVE_PATH}/{FILE_ACTIVE_NAME_BASE}"
sh: true,
cwd: /{FILE_ACTIVE_PATH}

结尾:

vala的文档写得很详细,通俗易懂,示例代码也是一步又一步不比qt的sample粒度差,这语言也足够精练,就如同C#一样语言OO的格式化很好,做本地一般应用开发还是轻松易用,话又说回来了,一般人哪有那么多大应用等着开发,能朴素的解决问题就好。

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表