Skip to content

Commit 545b878

Browse files
committed
update .wasm build
1 parent bdc612c commit 545b878

14 files changed

+169
-193
lines changed

.github/workflows/blank.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ jobs:
202202
../v.exe . -o v2w.exe
203203
git clone https://github.com/pisaiah/vpaint --depth 1
204204
dir
205-
./v2w.exe ../ vpaint
205+
./v2w.exe ../ vpaint -Os
206206
- name: Remove excluded
207207
run: |
208208
rm -rf .git

docs/demo/app.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/demo/app.wasm

9.72 KB
Binary file not shown.

docs/demo/app2.js

-16
This file was deleted.

docs/demo/app2.wasm

-622 KB
Binary file not shown.

docs/demo/bkup/app.js

+5-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/demo/bkup/app.wasm

65.1 KB
Binary file not shown.

docs/demo/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<canvas id="canvas" tabindex=-1></canvas>
2626
</div>
2727
<script id="appjs" src="./app.js"></script>
28-
<script type="module" src="mui_helper.js"></script>
28+
<script type="module" src="iui_helper.js"></script>
2929
<script>
3030
function calc_moble_size() {
3131
var w = window.innerWidth;

docs/demo/iui_helper.js

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// (c) 2024 Isaiah.
2+
import loadWASM from "./app.js";
3+
4+
function doo_load_files() {
5+
setTimeout(function() {
6+
for (var i = 0; i < localStorage.length; i++) { var key = localStorage.key(i);
7+
if (key.endsWith(".ttf")) { continue; }
8+
write_file(key.replace("//", "/"), localStorage.getItem(key));
9+
}
10+
}, 1);
11+
}
12+
13+
function write_file(a, b) {
14+
setTimeout(function() {
15+
var dirr = a.substring(0, a.lastIndexOf("/")); var dd = "";
16+
var spl = dirr.split("/");
17+
for (var i = 0; i < spl.length; i++) { var da = spl[i]; dd += da + "/"; try { iui.module.FS.mkdir(dd) } catch (exx) {} }
18+
try { iui.module.FS.mkdir(dirr) } catch (exx) {}
19+
iui.module.FS.writeFile(a, b);
20+
}, 1);
21+
}
22+
23+
function save_folder(pa) {
24+
setTimeout(function() { save_folder_2(pa); }, 1); // wasm crashes if we load FS too early
25+
}
26+
27+
function save_folder_2(pa) {
28+
var is_file = iui.module.FS.isFile(iui.module.FS.stat(pa).mode)
29+
if (!is_file) {
30+
var last = pa.substring(pa.lastIndexOf("/") + 1, pa.length);
31+
if (last.length <= 2 && last.includes(".")) { return; }
32+
var lss = iui.module.FS.readdir(pa); for (var i = 0; i < lss.length; i++) { save_folder_2(pa + "/" + lss[i]); }
33+
} else { var con = iui.module.FS.readFile(pa, { encoding: "utf8" }); localStorage.setItem(pa, con); }
34+
}
35+
36+
window.iui = {
37+
module: null,
38+
latest_file: null,
39+
task_result: "0",
40+
open_file_dialog: async () => {
41+
let input = document.createElement("input");
42+
input.type = "file";
43+
await new Promise((promise_resolve, promise_reject) => {
44+
input.addEventListener("change", async e => {
45+
iui.latest_file = e.target.files[0];
46+
let arr_buf = await iui.latest_file.arrayBuffer();
47+
iui.module.FS.writeFile(iui.latest_file.name, new Uint8Array(arr_buf));
48+
promise_resolve();
49+
});
50+
input.click();
51+
});
52+
iui.task_result = "1";
53+
return iui.latest_file.name;
54+
},
55+
save_file_dialog: async () => {
56+
iui.latest_file = {name: prompt("File Name to save to")};
57+
try { iui.module.FS.unlink(iui.latest_file.name); } catch (error) {}
58+
iui.task_result = "1"; iui.watch_file_until_action();
59+
},
60+
download_file: (filename, uia) => {
61+
let blob = new Blob([uia], { type: "application/octet-stream" });
62+
let url = window.URL.createObjectURL(blob);
63+
let downloader = document.createElement("a");
64+
downloader.href = url; downloader.download = filename; downloader.click(); downloader.remove();
65+
setTimeout(() => { window.URL.revokeObjectURL(url); }, 1000);
66+
},
67+
watch_file_until_action: async () => {
68+
let fi_nam = iui.latest_file.name;
69+
let watcher = setInterval(() => {
70+
if(iui.module.FS.analyzePath(fi_nam).exists){ clearInterval(watcher); iui.download_file(fi_nam, iui.module.FS.readFile(fi_nam)); iui.module.FS.unlink(fi_nam); }
71+
}, 500);
72+
},
73+
set trigger(val){
74+
if (val == "openfiledialog"){ return iui.open_file_dialog(); } else if (val == "savefiledialog"){ iui.save_file_dialog(); }
75+
else if (val == "keyboard-hide"){ document.getElementById("canvas").focus(); navigator.virtualKeyboard.hide(); }
76+
else if (val == "keyboard-show"){ document.getElementById("canvas").focus(); navigator.virtualKeyboard.show(); }
77+
else if (val == "lloadfiles") { doo_load_files(); } else if (val == "savefiles") { save_folder("/home") }
78+
else if (val.indexOf("savefile=") != -1) { var fi_nam = val.split("savefile=")[1]; iui.download_file(fi_nam, iui.module.FS.readFile(fi_nam)); }
79+
}
80+
};
81+
82+
(async () => {
83+
iui.module = await loadWASM();
84+
})();
85+

docs/demo/mui_helper.js

-143
This file was deleted.

src/paint.v

+23-20
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,24 @@ mut:
1515
@[heap]
1616
struct App {
1717
mut:
18-
win &ui.Window
19-
sv &ui.ScrollView
20-
sidebar &ui.Panel // HBox
21-
canvas_zoom int
22-
data &ImageViewData
23-
canvas &Image
24-
color gx.Color
25-
color_2 gx.Color = gx.white
26-
sele_color bool
27-
tool &Tool
28-
ribbon &ui.Panel
29-
status_bar &ui.Panel
30-
stat_lbl &ui.Label
31-
brush_size int = 1
32-
bg_id int
33-
need_open bool
34-
settings &Settings
18+
win &ui.Window
19+
sv &ui.ScrollView
20+
sidebar &ui.Panel // HBox
21+
canvas_zoom int
22+
data &ImageViewData
23+
canvas &Image
24+
color gx.Color
25+
color_2 gx.Color = gx.white
26+
sele_color bool
27+
tool &Tool
28+
ribbon &ui.Panel
29+
status_bar &ui.Panel
30+
stat_lbl &ui.Label
31+
brush_size int = 1
32+
bg_id int
33+
need_open bool
34+
settings &Settings
35+
wasm_load_tick int
3536
}
3637

3738
fn (app &App) get_color() gx.Color {
@@ -73,7 +74,7 @@ fn main() {
7374
}
7475
window.id_map['app'] = app
7576

76-
app.settings_load() or { println(err) }
77+
app.settings_load() or { println('Error loading settings: ${err}') }
7778
app.make_menubar(mut window)
7879

7980
mut path := os.resource_abs_path('untitledv.png')
@@ -137,8 +138,10 @@ fn main() {
137138
cim := win.gg.cache_image(gg_im)
138139
app.bg_id = cim
139140

140-
// background := gx.rgb(210, 220, 240)
141-
// window.gg.set_bg_color(background)
141+
background := gx.rgb(210, 220, 240)
142+
window.gg.set_bg_color(background)
143+
144+
app.set_theme_bg(app.win.theme.name)
142145

143146
window.gg.run()
144147
}

src/ribbon.v

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ fn (mut app App) make_color_box() &ui.Panel {
6666
btn_color := btn.override_bg_color
6767
if btn_color != color {
6868
// WASM does not support Closures
69-
dump('Debug: Problem with Wasm closure')
69+
// dump('Debug: Problem with Wasm closure')
7070
app.set_color(btn_color)
7171
return
7272
}

src/settings.v

+29-5
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ fn (mut app App) show_settings() {
88

99
mut panel := ui.Panel.new(layout: ui.BoxLayout.new(ori: 1))
1010

11-
dump(app.settings)
12-
1311
mut box := ui.Checkbox.new(text: 'Auto-hide Sidebar')
1412
box.is_selected = app.settings.autohide_sidebar
1513
box.set_bounds(0, 0, 100, 24)
@@ -27,10 +25,31 @@ fn (mut app App) hide_sidebar_mouse_up(mut e ui.MouseEvent) {
2725
app.settings_save() or {}
2826
}
2927

30-
const default_config = ['# VPaint Configuration File']
28+
const default_config = ['# VPaint Configuration File', 'theme: Default']
29+
30+
fn wasm_save_files() {
31+
$if emscripten ? {
32+
C.emscripten_run_script(c'iui.trigger = "savefiles"')
33+
}
34+
}
35+
36+
fn wasm_load_files() {
37+
$if emscripten ? {
38+
C.emscripten_run_script(c'iui.trigger = "lloadfiles"')
39+
}
40+
}
41+
42+
fn get_cfg_dir() string {
43+
$if emscripten ? {
44+
return os.home_dir()
45+
}
46+
return os.config_dir() or { os.home_dir() }
47+
}
3148

3249
fn (mut app App) settings_load() ! {
33-
cfg_dir := os.config_dir() or { return err }
50+
wasm_load_files()
51+
52+
cfg_dir := get_cfg_dir()
3453
dir := os.join_path(cfg_dir, '.vpaint')
3554
file := os.join_path(dir, 'config.txt')
3655

@@ -62,12 +81,13 @@ fn (mut app App) settings_load() ! {
6281
mut theme := ui.theme_by_name(text)
6382
app.win.set_theme(theme)
6483
app.set_theme_bg(text)
84+
app.settings.theme = text
6585
}
6686
}
6787
}
6888

6989
fn (mut app App) settings_save() ! {
70-
cfg_dir := os.config_dir() or { return err }
90+
cfg_dir := get_cfg_dir()
7191
dir := os.join_path(cfg_dir, '.vpaint')
7292
file := os.join_path(dir, 'config.txt')
7393

@@ -84,4 +104,8 @@ fn (mut app App) settings_save() ! {
84104
txt << 'theme: ${app.settings.theme}'
85105

86106
os.write_file(file, txt.join('\n')) or { return err }
107+
108+
if app.wasm_load_tick > 25 {
109+
wasm_save_files()
110+
}
87111
}

0 commit comments

Comments
 (0)