Skip to content

Commit

Permalink
🚧 V2 W.I.P.
Browse files Browse the repository at this point in the history
✨ Add random position shortcut.
✨ Allow to not save settings in a file (only in memory).
✨Detect and display A1111 prompts saved in images.
🔒️ Add security checks on `change_path`.
🐛 Better behaviour on position change (method created).
🐛 Improve settings form behaviour when path is changed.
🐛 Fix deadlock issue on `change_path`.
🐛 Reset position when `update_files_list`.
⬆️ Dependencies upgrades.
  • Loading branch information
Jimskapt committed Apr 20, 2024
1 parent ad9f28e commit f32921c
Show file tree
Hide file tree
Showing 16 changed files with 628 additions and 305 deletions.
295 changes: 153 additions & 142 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ members = [
resolver = "2"

[workspace.dependencies]
serde = "1.0.197"
serde_json = "1.0.115"
serde = "1.0.198"
serde_json = "1.0.116"

[workspace.lints.rust]
unused_parens = "allow"
Expand Down
20 changes: 20 additions & 0 deletions src-front/ai_prompt.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!-- @format -->

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="stylesheet" href="styles.css" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>A.I. prompt</title>
<script src="/ai_prompt.js" defer></script>
</head>

<body id="ai_prompt_page">
<p><a href="/index.html">⇽ back</a></p>

<h2>🧠 A.I. prompt detected</h2>

<pre id="ai_prompt">Loading …</pre>
</body>
</html>
16 changes: 16 additions & 0 deletions src-front/ai_prompt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/** @format */

const { invoke } = window.__TAURI__.core;

window.addEventListener('DOMContentLoaded', async function (event) {
let current_path = await invoke('get_current_path');
let prompt = await invoke('get_ai_prompt');

if (prompt != null && prompt != undefined) {
this.document.querySelector('#ai_prompt').innerText =
current_path + '\n\n' + prompt;
} else {
this.document.querySelector('#ai_prompt').innerText =
current_path + '\n\n' + 'No prompt, or error.';
}
});
7 changes: 7 additions & 0 deletions src-front/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
<img src="http://image.localhost/" alt="" id="preview" />
</p>
<button id="next"></button>
<span id="bottom_tools">
<a href="/ai_prompt.html" id="ai_prompt">
<span id="ai_icon"></span>
<pre id="ai_content"></pre>
</a>
<span id="position_counter"></span>
</span>
<input id="preview_path" value="loading" />
</body>
</html>
31 changes: 24 additions & 7 deletions src-front/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,31 @@
const { invoke } = window.__TAURI__.core;

async function refresh() {
document.querySelector('#preview').src =
'http://image.localhost/?rand=' + Math.random() * 9999999;
document.querySelector('#preview_path').value = await invoke(
'get_current_path'
);
document.querySelector('#preview').src =
'http://image.localhost/?rand=' + Math.random() * 9999999;

var position = await invoke('get_current_position');
if (position === null || position === undefined) {
position = '?';
} else {
position += 1;
}

document.querySelector('#position_counter').innerText =
position + ' / ' + (await invoke('get_images_length'));

let ai_prompt = await invoke('get_ai_prompt');
if (ai_prompt != null && ai_prompt != undefined) {
document.querySelector('#ai_prompt #ai_content').innerText =
'🧠 A.I. prompt detected :\n\n' + ai_prompt;
document.querySelector('#ai_prompt #ai_icon').innerText = '🧠';
} else {
document.querySelector('#ai_prompt #ai_content').innerText = '';
document.querySelector('#ai_prompt #ai_icon').innerText = '';
}
}

async function change_path(event) {
Expand Down Expand Up @@ -78,9 +98,6 @@ window.addEventListener('DOMContentLoaded', async function (event) {
await change_position(+1);
});

let that = this;
that.setTimeout(async function () {
await invoke('update_files_list');
that.setTimeout(refresh, 500);
}, 500);
await invoke('update_files_list');
this.setTimeout(refresh, 500);
});
14 changes: 7 additions & 7 deletions src-front/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
<p><a href="/index.html">⇽ back</a></p>

<form>
<label for="settings_path" class="solo">💾 settings save file path</label>
<input
type="text"
id="settings_path"
onchange="set_modified(true)"
class="solo"
/>
<label for="settings_path" class="solo"
>💾 settings save file path
<i
>(set to empty to only temporary save in memory until closing)</i
></label
>
<input type="text" id="settings_path" class="solo" />

<div id="dyn_settings_form"></div>

Expand Down
39 changes: 31 additions & 8 deletions src-front/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,34 @@ const { invoke } = window.__TAURI__.core;

var modified = false;

function change_settings_path(event) {
if (!modified) {
invoke('set_settings_path', {
settingsPath: event.target.value,
});

setTimeout(function () {
location.reload(true);
}, 500);
} else {
set_modified(true);
}
}

window.addEventListener('DOMContentLoaded', function () {
document
.querySelector('#settings_path')
.addEventListener('blur', change_settings_path);
});

window.onbeforeunload = function () {
if (modified) {
return 'unsaved changes';
} else {
return null;
}
};

// used by wasm :
function set_modified(do_no_lock_save_path) {
modified = true;
Expand Down Expand Up @@ -34,11 +62,6 @@ function set_unmodified() {

document.querySelector('#settings_path').removeAttribute('readonly');
}

window.onbeforeunload = function () {
if (modified) {
return 'unsaved changes';
} else {
return null;
}
};
function is_modified() {
return modified;
}
42 changes: 40 additions & 2 deletions src-front/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,16 @@ body#index {
right: 0;
left: 0;
text-align: center;
height: 4vh;
margin: 0 1vw 2vh 1vw;
height: 3vh;
margin: 0 1vw 1vh 1vw;
z-index: 10;
}

#index #bottom_tools {
position: absolute;
bottom: calc(1vh + 3vh + 1.5em);
right: 1vw;
font-size: 0.8em;
z-index: 10;
}

Expand Down Expand Up @@ -189,6 +197,30 @@ body#index {

/* --------------------------------------------------- */

#index #ai_prompt #ai_content {
visibility: hidden;
background-color: black;
opacity: 0.9;
color: #fff;
border-radius: 1em;
padding: 1em;

position: fixed;
top: 10vh;
left: 10vw;
width: 80vw;
height: 80vh;
z-index: 100;

white-space: pre-wrap;
}

#index #ai_prompt:hover #ai_content {
visibility: visible;
}

/* --------------------------------------------------- */

#settings label.solo {
display: block;
margin-top: 2em;
Expand Down Expand Up @@ -263,6 +295,12 @@ body#index {

/* --------------------------------------------------- */

#ai_prompt_page pre {
white-space: pre-wrap;
}

/* --------------------------------------------------- */

@media (prefers-color-scheme: dark) {
:root {
color: #f6f6f6;
Expand Down
5 changes: 3 additions & 2 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,21 @@ crate-type = ["staticlib", "cdylib", "rlib"]
path = "src/lib/mod.rs"

[build-dependencies]
tauri-build = { version = "2.0.0-beta.11", features = [] }
tauri-build = { version = "2.0.0-beta.12", features = [] }

[dependencies]
common = { path = "../common" }

serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }

tauri = { version = "2.0.0-beta.14", features = [] }
tauri = { version = "2.0.0-beta.15", features = [] }
tauri-plugin-shell = "2.0.0-beta.3"
toml = "0.8.12"
tokio = { version = "1.37.0", features = ["rt-multi-thread", "macros", "fs", "time"] }
rand = "0.8.5"
trash = "4.1.0"
encoding_rs = "0.8.34"

[features]
# this feature is used for production builds or when `devPath` points to the filesystem
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/gen/schemas/acl-manifests.json

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions src-tauri/gen/schemas/desktop-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1241,6 +1241,13 @@
"webview:allow-set-webview-size"
]
},
{
"description": "webview:allow-set-webview-zoom -> Enables the set_webview_zoom command without any pre-configured scope.",
"type": "string",
"enum": [
"webview:allow-set-webview-zoom"
]
},
{
"description": "webview:allow-webview-close -> Enables the webview_close command without any pre-configured scope.",
"type": "string",
Expand Down Expand Up @@ -1318,6 +1325,13 @@
"webview:deny-set-webview-size"
]
},
{
"description": "webview:deny-set-webview-zoom -> Denies the set_webview_zoom command without any pre-configured scope.",
"type": "string",
"enum": [
"webview:deny-set-webview-zoom"
]
},
{
"description": "webview:deny-webview-close -> Denies the webview_close command without any pre-configured scope.",
"type": "string",
Expand Down
14 changes: 14 additions & 0 deletions src-tauri/gen/schemas/windows-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1241,6 +1241,13 @@
"webview:allow-set-webview-size"
]
},
{
"description": "webview:allow-set-webview-zoom -> Enables the set_webview_zoom command without any pre-configured scope.",
"type": "string",
"enum": [
"webview:allow-set-webview-zoom"
]
},
{
"description": "webview:allow-webview-close -> Enables the webview_close command without any pre-configured scope.",
"type": "string",
Expand Down Expand Up @@ -1318,6 +1325,13 @@
"webview:deny-set-webview-size"
]
},
{
"description": "webview:deny-set-webview-zoom -> Denies the set_webview_zoom command without any pre-configured scope.",
"type": "string",
"enum": [
"webview:deny-set-webview-zoom"
]
},
{
"description": "webview:deny-webview-close -> Denies the webview_close command without any pre-configured scope.",
"type": "string",
Expand Down
30 changes: 16 additions & 14 deletions src-tauri/src/lib/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use rand::{distributions::Alphanumeric, Rng};
#[derive(Debug, Clone)]
pub enum AppAction {
ChangePosition(isize),
ChangeRandomPosition,
Move(usize),
RestoreImage,
}
Expand Down Expand Up @@ -34,22 +35,23 @@ pub fn apply_action(state: Arc<RwLock<crate::AppState>>, action: &AppAction) ->
}

let new_position_usize: usize = new_position.try_into().unwrap();
state.write().unwrap().current_position = Some(new_position_usize);
state.write().unwrap().display_path = format!(
"{}",
state
.read()
.unwrap()
.images
.get(new_position_usize)
.unwrap()
.get_current()
.display()
);
state
.write()
.unwrap()
.set_position(Some(new_position_usize));

changed = (old_position != state.read().unwrap().current_position);
}
}
AppAction::ChangeRandomPosition => {
let max_val = state.read().unwrap().images.len();

let new_position = rand::thread_rng().gen_range(0..max_val);

state.write().unwrap().set_position(Some(new_position));

changed = true;
}
AppAction::Move(id) => {
let current_position = state.read().unwrap().current_position;
if let Some(position) = current_position {
Expand Down Expand Up @@ -102,7 +104,7 @@ pub fn apply_action(state: Arc<RwLock<crate::AppState>>, action: &AppAction) ->

if let Some(image) = state_w.images.get_mut(position) {
image.moved = Some(new_path);
state_w.display_path = format!("{}", image.get_current().display());
state_w.set_position(Some(position));
state_w.settings.steps_after_move
} else {
todo!()
Expand Down Expand Up @@ -167,7 +169,7 @@ pub fn apply_action(state: Arc<RwLock<crate::AppState>>, action: &AppAction) ->
let mut state_w = state.write().unwrap();
if let Some(image) = state_w.images.get_mut(position) {
image.moved = Some(new_path);
state_w.display_path = format!("{}", image.get_current().display())
state_w.set_position(Some(position));
}
}
}
Expand Down
Loading

0 comments on commit f32921c

Please sign in to comment.