LoginSignup
17
15

More than 5 years have passed since last update.

LinuxでもSwiftを使ってGUI開発をしたい!【SwiftGtk】

Posted at

はじめに

VALU Advent Calendar 2018,18日目の記事です!

Swift がオープンソース化され,Swift が Linux 上でも動作するようになって久しい今日。
いくら Swift で諸々が書けるようになったとしても,アプリ開発者として Cocoa UI に親しんだ方々には,CLI のみで動く Swift には物足りないと思う方もいらっしゃるのではないでしょうか。

そこで,今回は Linux でも動く Swift GUI,Gtk+ を Swift で扱うライブラリ,TomasLinhart/SwiftGtk の紹介を致します。

Ubuntu 環境構築

初期化されたUbuntuにささっと Swift 4.2.1 を入れていきます。

Terminal
$ sudo apt install -y wget clang libpython2.7 libxml2 curl libbsd-dev
$ wget https://swift.org/builds/swift-4.2.1-release/ubuntu1804/swift-4.2.1-$ RELEASE/swift-4.2.1-RELEASE-ubuntu18.04.tar.gz
$ tar -zxvf swift-4.2.1-RELEASE-ubuntu18.04.tar.gz
$ sudo mv swift-4.2.1-RELEASE-ubuntu18.04 /usr/local/swift
$ echo 'export PATH="/usr/local/swift/usr/bin:$PATH"' >> ~/.bash_profile
$ source ~/.bash_profile

以上の後,swift -v にて 「swift-4.2.1-RELEASE」 の文字が確認できれば完了です。

Gtk のインストール

Gtk は GIMP のために開発された multi-platform のGUIツールキットで,GNOME を始め,様々な部分で利用されています。

インストールは以下の通りです。

Terminal
sudo apt install -y libgtk-3-dev

Package.swift の編集

Package.swift の編集に(お好みで) vim,Package.swift の依存性解決のために git を導入します。

Terminal
$ sudo apt install -y vim git

Swift Package Manager を利用してプロジェクトを作成します。

Terminal
$ mkdir test && cd $_
$ swift package init --type executable

$ vi Package.swift

次に Package.swift を編集します。 SwiftGtk の他に追加すべきものがある場合は追加しましょう。

Package.swift
// swift-tools-version:4.2
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "test",
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        // .package(url: /* package url */, from: "1.0.0"),
        .package(url: "https://github.com/TomasLinhart/SwiftGtk", "0.3.1" ..< "0.4.0"),
    ],
    targets: [
        // Targets are the basic building blocks of a package. A target can define a module or a test suite.
        // Targets can depend on other targets in this package, and on products in packages which this package depends on.
        .target(
            name: "test",
            dependencies: ["SwiftGtk"]),
        .testTarget(
            name: "testTests",
            dependencies: ["test"]),
    ]
)

main.swift の編集

https://github.com/TomasLinhart/SwiftGtk#demo に従います。

main.swift
import SwiftGtk

let app = Application(applicationId: "com.example.application")
app.run { window in
    window.title = "Hello World"
    window.defaultSize = Size(width: 400, height: 400)
    window.resizable = true

    let button = Button(label: "Press Me")
    button.clicked = { _ in
        let newWindow = Window(windowType: .topLevel)
        newWindow.title = "Just a window"
        newWindow.defaultSize = Size(width: 200, height: 200)
        let labelPressed = Label(text: "Oh, you pressed the button.")
        newWindow.add(labelPressed)

        newWindow.showAll()
    }

    window.add(button)
}

裏にタイポによるビルドエラーが見られますが,Swift でビルド,実行させたものが以下となります。

ss 2018-12-18 at 14.18.00.png

注意

今回Swiftから利用した TomasLinhart/SwiftGtk については MIT license にて配布されておりますが,一方で GTK+ 自体は

GTK+ is licensed under the GNU LGPL 2.1

LGPL の下で配布されており,注意が必要です。

「今年の」まとめ

VALU Advent Calendar 2018 にて Swift の魅力をお伝えしてきました。RxSwift や設計の話も好きですが,フィジカルコンピューティングやCocoa以外でのGUI開発など,少し変わった使い方も紹介致しました。

少しでも読者の皆さまに興味を持っていただけるきっかけに触れ,Swift の魅力をお伝えできればなと思います。

まだ今年はちょうど2週間ありますが,現在も,来年以降も,どんどん VALU のアプリは進化していきますので,応援の程よろしくお願い致します。

参照

17
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
15