🔧 ui: minor refactoring

- remove helper `loading_success`
- typing in `ThouCanvas`
-  internal renamings and logic optimizations
This commit is contained in:
Jörn-Michael Miehe 2025-12-28 02:34:41 +00:00
parent 2aff1f3a99
commit 7643f35ec5
4 changed files with 23 additions and 26 deletions

View file

@ -25,7 +25,7 @@ const message = useTemplateRef("message");
onMounted(() =>
emit("handle", {
show(options: ToastOptions = {}) {
if (!(message.value instanceof HTMLElement)) return;
if (message.value === null) return;
toast({
...options,

View file

@ -20,7 +20,7 @@
</template>
<script setup lang="ts">
import { VueLike, loading_success } from "@/lib/helpers";
import { VueLike, unwrap_loading } from "@/lib/helpers";
import { Rectangle } from "@/lib/rects/rectangle";
import { advent22Store } from "@/lib/store";
import { computed } from "vue";
@ -47,9 +47,11 @@ withDefaults(
);
const aspect_ratio = computed(() => {
if (!loading_success(store.background_image)) return 1;
return store.background_image.aspect_ratio;
try {
return unwrap_loading(store.background_image).aspect_ratio;
} catch {
return 1;
}
});
</script>

View file

@ -29,21 +29,23 @@ function get_event_thous(event: MouseEvent): Vector2D {
);
}
type TCEventType = "mousedown" | "mousemove" | "mouseup" | "click" | "dblclick";
const is_tceventtype = (t: unknown): t is TCEventType =>
t === "mousedown" ||
t === "mousemove" ||
t === "mouseup" ||
t === "click" ||
t === "dblclick";
const emit = defineEmits<{
(event: "mousedown", e: MouseEvent, point: Vector2D): void;
(event: "mouseup", e: MouseEvent, point: Vector2D): void;
(event: "mousemove", e: MouseEvent, point: Vector2D): void;
(event: "click", e: MouseEvent, point: Vector2D): void;
(event: "dblclick", e: MouseEvent, point: Vector2D): void;
(event: TCEventType, e: MouseEvent, point: Vector2D): void;
}>();
function transform_mouse_event(event: MouseEvent) {
const point = get_event_thous(event);
if (!is_tceventtype(event.type)) return;
// mute a useless typescript error
const event_type = event.type as "mousedown";
emit(event_type, event, point);
emit(event.type, event, get_event_thous(event));
}
</script>

View file

@ -20,29 +20,22 @@ export function unwrap_vuelike<T>(value: VueLike<T>): T {
export type Loading<T> = T | "loading" | "error";
export function loading_success<T>(o: Loading<T>): o is T {
if (o === "loading") return false;
if (o === "error") return false;
return true;
}
export function unwrap_loading<T>(o: Loading<T>): T {
if (!loading_success(o)) throw null;
if (o === "loading" || o === "error") throw null;
return o;
}
export function wait_for(condition: () => boolean, action: () => void) {
const do_action = () => {
const enqueue_action = () => {
if (!condition()) {
nextTick(do_action);
nextTick(enqueue_action);
return;
}
action();
};
do_action();
enqueue_action();
}
export function handle_error(error: unknown) {