Compare commits

...

7 Commits

23 changed files with 329 additions and 170 deletions

View File

@@ -4,7 +4,7 @@
import PackageDescription
let package = Package(
name: "Adwaita Template",
name: "Poor Man's Calculator",
platforms: [
.macOS(.v13)
],
@@ -14,18 +14,32 @@ let package = Package(
],
targets: [
.executableTarget(
name: "AdwaitaTemplate",
name: "PMCalc",
dependencies: [
.product(name: "Adwaita", package: "adwaita-swift"),
.product(name: "Localized", package: "localized")
.product(name: "Localized", package: "localized"),
"CMath"
],
path: "Sources",
path: "Sources/PMCalc",
resources: [
.process("Localized.yml")
],
cSettings: [
.headerSearchPath("Sources/CMath/include")
],
linkerSettings: [
.unsafeFlags([
"-L./Sources/CMath/lib/"
])
],
plugins: [
.plugin(name: "GenerateLocalized", package: "localized")
]
),
.systemLibrary(
name: "CMath",
path: "Sources/CMath",
providers: []
)
],
swiftLanguageModes: [.v5]

View File

@@ -1,62 +1,7 @@
<p align="center">
<img width="256" alt="Adwaita Template Icon" src="data/icons/io.github.AparokshaUI.AdwaitaTemplate.svg">
<h1 align="center">Adwaita Template</h1>
<h1 align="center">Bachelorproef | Fase 4</h1>
</p>
_Adwaita Template_ is a template application for the [Adwaita for Swift package](https://adwaita-swift.aparoksha.dev/documentation/adwaita/).
Deze repository bevat PMCalc, een simpele calculator-app, geschreven met [Adwaita for Swift](https://adwaita-swift.aparoksha.dev/documentation/adwaita/) en CMath.
## Table of Contents
- [Installation](#Installation)
- [Usage](#Usage)
- [Thanks](#Thanks)
## Installation
Install the [GNOME Builder](https://flathub.org/apps/org.gnome.Builder) IDE.
For designing an app icon, [App Icon Preview](https://flathub.org/apps/org.gnome.design.AppIconPreview) and [Inkscape](https://flathub.org/apps/org.inkscape.Inkscape) are recommended.
> [!NOTE]
> You do not have to install any dependencies of Adwaita for Swift, including Swift, on your system.
> The Adwaita template runs in a [Flatpak](https://flatpak.org/).
> The GNOME Builder will automatically download dependencies from [Flathub](https://flathub.org).
> You can also use [Visual Studio Code](https://code.visualstudio.com/) with a Docker [dev container](https://code.visualstudio.com/docs/devcontainers/containers) hosting the Swift toolchain and Adwaita libraries.
## Usage
1. Open this project in the GNOME Builder. It will start downloading the dependencies.
- GNOME Builder will start downloading dependencies when opened.
- For Visual Studio Code, enable the [Dev Containers extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers), the [Swift extension](https://marketplace.visualstudio.com/items?itemName=sswg.swift-lang) and the [CodeLLDB extension](https://marketplace.visualstudio.com/items?itemName=vadimcn.vscode-lldb) and reopen the project window with the dev container. It may take a couple minutes to build.
2. Build and run the application.
- For GNOME Builder, use the "run" icon in the toolbar.
- For Visual Studio Code, select the "run and debug" tab in the leftmost sidebar, and then tap the run icon next to "Debug AdwaitaTemplate".
3. Change the app's name and other information about the application in the following files (and file names):
- `README.md`
- `Package.swift`
- `io.github.AparokshaUI.AdwaitaTemplate.json`
- `Sources/AdwaitaTemplate.swift`
- `data/io.github.AparokshaUI.AdwaitaTemplate.metainfo.xml`
- `data/io.github.AparokshaUI.AdwaitaTemplate.desktop`
- `data/icons/io.github.AparokshaUI.AdwaitaTemplate.Source.svg`
- `data/icons/io.github.AparokshaUI.AdwaitaTemplate.svg`
- `data/icons/io.github.AparokshaUI.AdwaitaTemplate-symbolic.svg`
4. Edit the code. Help is available [here](https://adwaita-swift.aparoksha.dev/), ask questions in the [forums](https://forums.aparoksha.dev/).
5. You can edit the app's icons using the previously installed tools according to [this](https://blogs.gnome.org/tbernard/2019/12/30/designing-an-icon-for-your-app/) tutorial.
6. In GNOME Builder, click on the dropdown next to the hammer and then on `Export`. Wait until the file manager appears, open the `.flatpak` file and install the app on your device!
7. If you want to publish your app, replace `debug` in the following build commands by `release`:
```
"swift build -c debug --static-swift-stdlib",
"install -Dm755 .build/debug/AdwaitaTemplate /app/bin/AdwaitaTemplate",
```
### Flatpak SPM Generator
If you want to e.g. publish your app on Flathub where no internet connection is allowed while running the build commands,
you can use [this tool](https://github.com/flatpak/flatpak-builder-tools/tree/master/spm) that lets you generate a Flatpak manifest JSON from a Swift package.
## Thanks
### Dependencies
- [Adwaita for Swift](https://git.aparoksha.dev/aparoksha/adwaita-swift) licensed under the [MIT License](https://git.aparoksha.dev/aparoksha/adwaita-swift/src/branch/main/LICENSE.md)
- [Localized](https://git.aparoksha.dev/aparoksha/localized) licensed under the [MIT License](https://git.aparoksha.dev/aparoksha/localized/src/branch/main/LICENSE.md)
Als GUI maakt deze applicatie gebruik van de Calculator-UI die gemaakt werd voor [Fase 2](https://repo.thealgorithm476.me/Bachelorproef/bap-poc-phase-2) van deze bachelerproef. Als domeinlogica wordt CMath gebruikt, dat werd ontwikkeld om interoperability tussen Swift en C te testen in [Fase 3](https://repo.thealgorithm476.me/Bachelorproef/bap-poc-phase-3).

View File

@@ -0,0 +1,13 @@
//
// add_nums.h
// CMath
//
// Created by TheAlgorithm476 on 28/03/2025.
//
#ifndef __ADD_NUMS_H
#define __ADD_NUMS_H
double add_nums(double a, double b);
#endif // __ADD_NUMS_H

View File

@@ -0,0 +1,13 @@
//
// div_nums.h
// CMath
//
// Created by TheAlgorithm476 on 28/03/2025.
//
#ifndef __DIV_NUMS_H
#define __DIV_NUMS_H
double div_nums(double a, double b);
#endif // __DIV_NUMS_H

View File

@@ -0,0 +1,13 @@
//
// mul_nums.h
// CMath
//
// Created by TheAlgorithm476 on 28/03/2025.
//
#ifndef __MUL_NUMS_H
#define __MUL_NUMS_H
double mul_nums(double a, double b);
#endif // __MUL_NUMS_H

View File

@@ -0,0 +1,17 @@
//
// spc_nums.h
// CMath
//
// Created by TheAlgorithm476 on 28/03/2025.
//
#ifndef __SPC_NUMS_H
#define __SPC_NUMS_H
#define POOR_MANS_POW_GUESSES 12
#define NEWTONIAN_DIV_GUESSES 12
double pow(double x, double exp);
double sqrt(double x);
#endif // __SPC_NUMS_H

View File

@@ -0,0 +1,13 @@
//
// sub_nums.h
// CMath
//
// Created by TheAlgorithm476 on 28/03/2025.
//
#ifndef __SUB_NUMS_H
#define __SUB_NUMS_H
double sub_nums(double a, double b);
#endif // __SUB_NUMS_H

BIN
Sources/CMath/lib/libcmath.so Executable file

Binary file not shown.

View File

@@ -0,0 +1,9 @@
module CMath {
header "include/add_nums.h"
header "include/sub_nums.h"
header "include/mul_nums.h"
header "include/div_nums.h"
header "include/spc_nums.h"
export *
link "cmath"
}

View File

@@ -0,0 +1,8 @@
enum Operator {
case add
case sub
case mul
case div
case pow
case sqrt
}

View File

@@ -2,89 +2,130 @@
// https://docs.swift.org/swift-book
import Adwaita
import CMath
@main
struct AdwaitaTemplate: App {
let app = AdwaitaApp(id: "io.github.AparokshaUI.AdwaitaTemplate")
struct PMCalc: App {
let app = AdwaitaApp(id: "me.thealgorithm476.PMCalc.PMCalc")
@State
var vm = ViewModel()
var scene: Scene {
Window(id: "main") { window in
VStack(spacing: 8) {
Text("Calculations here...")
Text(vm.display)
.selectable(false)
.title2()
.halign(.end)
VStack(spacing: 8) {
HStack(spacing: 8) {
Button("AC") {}
Button("AC") {
vm.clear()
}
.keyboardShortcut("c", app: app)
.hexpand()
.destructive()
Button("") {}
Button("^") {
vm.operation(.pow)
}
.hexpand()
.flat()
Button("%") {}
.keyboardShortcut("percent", app: app)
.hexpand()
Button("/") {}
Button("/") {
vm.operation(.div)
}
.keyboardShortcut("slash", app: app)
.hexpand()
}
.vexpand()
HStack(spacing: 8) {
Button("7") {}
Button("7") {
vm.addNumber(7)
}
.keyboardShortcut("7", app: app)
.hexpand()
Button("8") {}
Button("8") {
vm.addNumber(8)
}
.keyboardShortcut("8", app: app)
.hexpand()
Button("9") {}
Button("9") {
vm.addNumber(9)
}
.keyboardShortcut("9", app: app)
.hexpand()
Button("*") {}
Button("*") {
vm.operation(.mul)
}
.keyboardShortcut("asterisk", app: app)
.hexpand()
}
.vexpand()
HStack(spacing: 8) {
Button("4") {}
Button("4") {
vm.addNumber(4)
}
.keyboardShortcut("4", app: app)
.hexpand()
Button("5") {}
Button("5") {
vm.addNumber(5)
}
.keyboardShortcut("5", app: app)
.hexpand()
Button("6") {}
Button("6") {
vm.addNumber(6)
}
.keyboardShortcut("6", app: app)
.hexpand()
Button("-") {}
Button("-") {
vm.operation(.sub)
}
.keyboardShortcut("minus", app: app)
.hexpand()
}
.vexpand()
HStack(spacing: 8) {
Button("1") {}
Button("1") {
vm.addNumber(1)
}
.keyboardShortcut("1", app: app)
.hexpand()
Button("2") {}
Button("2") {
vm.addNumber(2)
}
.keyboardShortcut("2", app: app)
.hexpand()
Button("3") {}
Button("3") {
vm.addNumber(3)
}
.keyboardShortcut("3", app: app)
.hexpand()
Button("+") {}
Button("+") {
vm.operation(.add)
}
.keyboardShortcut("plus", app: app)
.hexpand()
}
.vexpand()
HStack(spacing: 8) {
Button("0") {}
Button("0") {
vm.addNumber(0)
}
.keyboardShortcut("0", app: app)
.hexpand()
Button("") {}
Button("sq") {
vm.operation(.sqrt)
}
.hexpand()
.flat()
Button(",") {}
Button(",") {
vm.addComma()
}
.keyboardShortcut("comma", app: app)
.hexpand()
Button("=") {}
Button("=") {
vm.calculate()
}
.keyboardShortcut("equal", app: app)
.hexpand()
}

View File

@@ -27,12 +27,11 @@ struct ToolbarView: View {
.tooltip(Loc.mainMenu)
.aboutDialog(
visible: $about,
app: "AdwaitaTemplate",
developer: "david-swift",
version: "dev",
icon: .custom(name: "io.github.AparokshaUI.AdwaitaTemplate"),
website: .init(string: "https://git.aparoksha.dev/aparoksha/adwaita-template")!,
issues: .init(string: "https://git.aparoksha.dev/aparoksha/adwaita-template/issues")!
app: "PMCalc",
developer: "TheAlgorithm476",
version: "1.0",
icon: .custom(name: "me.thealgorithm476.PMCalc.PMCalc"),
website: .init(string: "https://repo.thealgorithm476.me/Bachelorproef/bap-poc-phase-4")!
)
}
}

View File

@@ -0,0 +1,73 @@
import Adwaita
import CMath
struct ViewModel {
private var numberBuffer: String = ""
private var number1: Double? = nil
private var number2: Double? = nil
private var op: Operator? = nil
@State
private(set) var display: String = ""
mutating func clear() {
numberBuffer = ""
number1 = nil
number2 = nil
op = nil
display = ""
}
mutating func addComma() {
guard !numberBuffer.isEmpty else { return }
numberBuffer.append(",")
display = numberBuffer
}
mutating func addNumber(_ number: UInt8) {
if numberBuffer.isEmpty { display = "" } // Reset display after new number is added to cleared buffer
numberBuffer.append(String(number))
display = numberBuffer
}
mutating func operation(_ op: Operator) {
guard !numberBuffer.isEmpty && number1 == nil else { return }
self.op = op
if number1 == nil { number1 = Double(numberBuffer) }
else { number2 = Double(numberBuffer) }
numberBuffer = ""
if op == .sqrt {
display = String(sqrt(number1!))
number1 = nil
self.op = nil
}
}
mutating func calculate() {
number2 = Double(numberBuffer)
numberBuffer = ""
var result: Double = 0.0
switch op {
case .add:
result = add_nums(number1!, number2!)
case .sub:
result = sub_nums(number1!, number2!)
case .mul:
result = mul_nums(number1!, number2!)
case .div:
result = div_nums(number1!, number2!)
case .pow:
result = pow(number1!, number2!)
case .sqrt, .none:
// do nothing
print("Attempting to apply Square Root to nothing!")
}
display = String(result)
}
}

View File

Before

Width:  |  Height:  |  Size: 574 B

After

Width:  |  Height:  |  Size: 574 B

View File

Before

Width:  |  Height:  |  Size: 175 KiB

After

Width:  |  Height:  |  Size: 175 KiB

View File

Before

Width:  |  Height:  |  Size: 1.5 KiB

After

Width:  |  Height:  |  Size: 1.5 KiB

View File

@@ -1,11 +0,0 @@
[Desktop Entry]
Version=1.0
Type=Application
Name=Adwaita Template
Comment=A template for creating GNOME apps with Swift
Categories=Development;GNOME;
Icon=io.github.AparokshaUI.AdwaitaTemplate
Exec=AdwaitaTemplate
Terminal=false

View File

@@ -0,0 +1,11 @@
[Desktop Entry]
Version=1.0
Type=Application
Name=Poor Man's Calculator
Comment=Simple Calculator written using Adwaita for Swift
Categories=Development;GNOME;
Icon=me.thealgorithm476.PMCalc.PMCalc
Exec=PMCalc
Terminal=false

View File

@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<component type="desktop-application">
<id>io.github.AparokshaUI.AdwaitaTemplate</id>
<id>me.thealgorithm476.PMCalc.PMCalc</id>
<name>Adwaita Template</name>
<summary>A template for creating GNOME apps with Swift</summary>
<name>Poor Man's Calculator</name>
<summary>Simple Calculator written using Adwaita for Swift</summary>
<metadata_license>MIT</metadata_license>
<project_license>LGPL-3.0-or-later</project_license>
@@ -16,10 +16,10 @@
<description>
<p>
This is a <em>long</em> description of this project. Yes - that is required!
Calculator written in Adwaita for Swift, using a custom Math library written in C.
</p>
</description>
<launchable type="desktop-id">io.github.AparokshaUI.AdwaitaTemplate.desktop</launchable>
<launchable type="desktop-id">me.thealgorithm476.PMCalc.PMCalc.desktop</launchable>
</component>

View File

@@ -1,12 +1,12 @@
{
"app-id": "io.github.AparokshaUI.AdwaitaTemplate",
"app-id": "me.thealgorithm476.PMCalc.PMCalc",
"runtime": "org.gnome.Platform",
"runtime-version": "46",
"sdk": "org.gnome.Sdk",
"sdk-extensions": [
"org.freedesktop.Sdk.Extension.swift5"
],
"command": "AdwaitaTemplate",
"command": "PMCalc",
"finish-args": [
"--share=ipc",
"--socket=fallback-x11",
@@ -30,7 +30,7 @@
],
"modules": [
{
"name": "AdwaitaTemplate",
"name": "PMCalc",
"builddir": true,
"buildsystem": "simple",
"sources": [
@@ -42,12 +42,12 @@
"build-commands": [
"./setup-offline.sh",
"swift build -c release --static-swift-stdlib --skip-update",
"strip .build/release/AdwaitaTemplate",
"install -Dm755 .build/release/AdwaitaTemplate /app/bin/AdwaitaTemplate",
"install -Dm644 data/io.github.AparokshaUI.AdwaitaTemplate.metainfo.xml $DESTDIR/app/share/metainfo/io.github.AparokshaUI.AdwaitaTemplate.metainfo.xml",
"install -Dm644 data/io.github.AparokshaUI.AdwaitaTemplate.desktop $DESTDIR/app/share/applications/io.github.AparokshaUI.AdwaitaTemplate.desktop",
"install -Dm644 data/icons/io.github.AparokshaUI.AdwaitaTemplate.svg $DESTDIR/app/share/icons/hicolor/scalable/apps/io.github.AparokshaUI.AdwaitaTemplate.svg",
"install -Dm644 data/icons/io.github.AparokshaUI.AdwaitaTemplate-symbolic.svg $DESTDIR/app/share/icons/hicolor/symbolic/apps/io.github.AparokshaUI.AdwaitaTemplate-symbolic.svg"
"strip .build/release/PMCalc",
"install -Dm755 .build/release/PMCalc /app/bin/PMCalc",
"install -Dm644 data/me.thealgorithm476.PMCalc.PMCalc.metainfo.xml $DESTDIR/app/share/metainfo/me.thealgorithm476.PMCalc.PMCalc.metainfo.xml",
"install -Dm644 data/me.thealgorithm476.PMCalc.PMCalc.desktop $DESTDIR/app/share/applications/me.thealgorithm476.PMCalc.PMCalc.desktop",
"install -Dm644 data/icons/me.thealgorithm476.PMCalc.PMCalc.svg $DESTDIR/app/share/icons/hicolor/scalable/apps/me.thealgorithm476.PMCalc.PMCalc.svg",
"install -Dm644 data/icons/me.thealgorithm476.PMCalc.PMCalc-symbolic.svg $DESTDIR/app/share/icons/hicolor/symbolic/apps/me.thealgorithm476.PMCalc.PMCalc-symbolic.svg"
]
}
]

View File

@@ -1,53 +0,0 @@
{
"app-id": "io.github.AparokshaUI.AdwaitaTemplate",
"runtime": "org.gnome.Platform",
"runtime-version": "48",
"sdk": "org.gnome.Sdk",
"sdk-extensions": [
"org.freedesktop.Sdk.Extension.swift6"
],
"command": "AdwaitaTemplate",
"finish-args": [
"--share=ipc",
"--socket=fallback-x11",
"--device=dri",
"--socket=wayland"
],
"build-options": {
"append-path": "/usr/lib/sdk/swift6/bin",
"prepend-ld-library-path": "/usr/lib/sdk/swift6/lib"
},
"cleanup": [
"/include",
"/lib/pkgconfig",
"/man",
"/share/doc",
"/share/gtk-doc",
"/share/man",
"/share/pkgconfig",
"*.la",
"*.a"
],
"modules": [
{
"name": "AdwaitaTemplate",
"builddir": true,
"buildsystem": "simple",
"sources": [
{
"type": "dir",
"path": "."
}
],
"build-commands": [
"swift build -c debug --static-swift-stdlib",
"strip .build/debug/AdwaitaTemplate",
"install -Dm755 .build/debug/AdwaitaTemplate /app/bin/AdwaitaTemplate",
"install -Dm644 data/io.github.AparokshaUI.AdwaitaTemplate.metainfo.xml $DESTDIR/app/share/metainfo/io.github.AparokshaUI.AdwaitaTemplate.metainfo.xml",
"install -Dm644 data/io.github.AparokshaUI.AdwaitaTemplate.desktop $DESTDIR/app/share/applications/io.github.AparokshaUI.AdwaitaTemplate.desktop",
"install -Dm644 data/icons/io.github.AparokshaUI.AdwaitaTemplate.svg $DESTDIR/app/share/icons/hicolor/scalable/apps/io.github.AparokshaUI.AdwaitaTemplate.svg",
"install -Dm644 data/icons/io.github.AparokshaUI.AdwaitaTemplate-symbolic.svg $DESTDIR/app/share/icons/hicolor/symbolic/apps/io.github.AparokshaUI.AdwaitaTemplate-symbolic.svg"
]
}
]
}

View File

@@ -0,0 +1,54 @@
{
"app-id": "me.thealgorithm476.PMCalc.PMCalc",
"runtime": "org.gnome.Platform",
"runtime-version": "48",
"sdk": "org.gnome.Sdk",
"sdk-extensions": [
"org.freedesktop.Sdk.Extension.swift6"
],
"command": "PMCalc",
"finish-args": [
"--share=ipc",
"--socket=fallback-x11",
"--device=dri",
"--socket=wayland"
],
"build-options": {
"append-path": "/usr/lib/sdk/swift6/bin",
"prepend-ld-library-path": "/usr/lib/sdk/swift6/lib"
},
"cleanup": [
"/include",
"/lib/pkgconfig",
"/man",
"/share/doc",
"/share/gtk-doc",
"/share/man",
"/share/pkgconfig",
"*.la",
"*.a"
],
"modules": [
{
"name": "PMCalc",
"builddir": true,
"buildsystem": "simple",
"sources": [
{
"type": "dir",
"path": "."
}
],
"build-commands": [
"swift build -c debug --static-swift-stdlib",
"strip .build/debug/PMCalc",
"install -Dm755 .build/debug/PMCalc /app/bin/PMCalc",
"install -Dm644 data/me.thealgorithm476.PMCalc.PMCalc.metainfo.xml $DESTDIR/app/share/metainfo/me.thealgorithm476.PMCalc.PMCalc.metainfo.xml",
"install -Dm644 data/me.thealgorithm476.PMCalc.PMCalc.desktop $DESTDIR/app/share/applications/me.thealgorithm476.PMCalc.PMCalc.desktop",
"install -Dm644 data/icons/me.thealgorithm476.PMCalc.PMCalc.svg $DESTDIR/app/share/icons/hicolor/scalable/apps/me.thealgorithm476.PMCalc.PMCalc.svg",
"install -Dm644 data/icons/me.thealgorithm476.PMCalc.PMCalc-symbolic.svg $DESTDIR/app/share/icons/hicolor/symbolic/apps/me.thealgorithm476.PMCalc.PMCalc-symbolic.svg",
"install -Dm644 Sources/CMath/lib/libcmath.so /app/lib/libcmath.so"
]
}
]
}