Refactor RadioInput widget (#332)

This commit is contained in:
Keavon Chambers 2021-08-07 03:15:40 -07:00
parent 699562b735
commit 434695d578
3 changed files with 80 additions and 73 deletions

View File

@ -34,11 +34,7 @@
<Separator :type="SeparatorType.Unrelated" />
<RadioInput @update:index="viewModeChanged" v-model:index="viewModeIndex">
<IconButton :action="() => {}" :icon="'ViewModeNormal'" :size="24" title="View Mode: Normal" />
<IconButton :action="() => comingSoon(319)" :icon="'ViewModeOutline'" :size="24" title="View Mode: Outline" />
<IconButton :action="() => comingSoon(320)" :icon="'ViewModePixels'" :size="24" title="View Mode: Pixels" />
</RadioInput>
<RadioInput :entries="viewModeEntries" v-model:selectedIndex="viewModeIndex" />
<PopoverButton>
<h3>View Mode</h3>
<p>More view mode options will be here</p>
@ -228,7 +224,7 @@ import PersistentScrollbar, { ScrollbarDirection } from "@/components/widgets/sc
import CanvasRuler, { RulerDirection } from "@/components/widgets/rulers/CanvasRuler.vue";
import IconButton from "@/components/widgets/buttons/IconButton.vue";
import PopoverButton from "@/components/widgets/buttons/PopoverButton.vue";
import RadioInput from "@/components/widgets/inputs/RadioInput.vue";
import RadioInput, { RadioEntries } from "@/components/widgets/inputs/RadioInput.vue";
import NumberInput, { IncrementDirection } from "@/components/widgets/inputs/NumberInput.vue";
import DropdownInput from "@/components/widgets/inputs/DropdownInput.vue";
import OptionalInput from "@/components/widgets/inputs/OptionalInput.vue";
@ -238,10 +234,15 @@ import { SectionsOfMenuListEntries } from "@/components/widgets/floating-menus/M
const documentModeEntries: SectionsOfMenuListEntries = [
[
{ label: "Design Mode", icon: "ViewportDesignMode" },
{ label: "Select Mode", icon: "ViewportSelectMode" },
{ label: "Guide Mode", icon: "ViewportGuideMode" },
{ label: "Select Mode", icon: "ViewportSelectMode", action: () => comingSoon(330) },
{ label: "Guide Mode", icon: "ViewportGuideMode", action: () => comingSoon(331) },
],
];
const viewModeEntries: RadioEntries = [
{ value: "normal", icon: "ViewModeNormal", tooltip: "View Mode: Normal" },
{ value: "outline", icon: "ViewModeOutline", tooltip: "View Mode: Outline", action: () => comingSoon(319) },
{ value: "pixels", icon: "ViewModePixels", tooltip: "View Mode: Pixels", action: () => comingSoon(320) },
];
const wasm = import("@/../wasm/pkg");
@ -294,12 +295,6 @@ export default defineComponent({
const { select_tool } = await wasm;
select_tool(toolName);
},
async viewModeChanged(toolIndex: number) {
function todo(_: number) {
return _;
}
todo(toolIndex);
},
async swapWorkingColors() {
const { swap_colors } = await wasm;
swap_colors();
@ -361,6 +356,7 @@ export default defineComponent({
canvasSvgHeight: "100%",
activeTool: "Select",
documentModeEntries,
viewModeEntries,
documentModeSelectionIndex: 0,
viewModeIndex: 0,
snappingEnabled: true,

View File

@ -183,7 +183,8 @@ const MenuList = defineComponent({
if (menuEntry.action) menuEntry.action();
else if (this.defaultAction) this.defaultAction();
else this.$emit("update:activeEntry", menuEntry);
this.$emit("update:activeEntry", menuEntry);
},
handleEntryMouseEnter(menuEntry: MenuListEntry) {
if (!menuEntry.children || !menuEntry.children.length) return;

View File

@ -1,89 +1,99 @@
<template>
<div class="radio-input" ref="radioInput">
<slot></slot>
<button :class="{ active: index === selectedIndex }" v-for="(entry, index) in entries" :key="index" @click="handleEntryClick(entry)" :title="entry.tooltip">
<IconLabel v-if="entry.icon" :icon="entry.icon" />
<TextLabel v-if="entry.label">{{ entry.label }}</TextLabel>
</button>
</div>
</template>
<style lang="scss">
.radio-input {
.popover-button button {
border-radius: 0;
}
& > * {
border-radius: 0;
margin: 0;
&:first-child,
&:first-child button {
border-radius: 2px 0 0 2px;
}
&:last-child,
&:last-child button {
border-radius: 0 2px 2px 0;
}
& + * {
margin-left: 1px;
}
}
& > button {
button {
background: var(--color-5-dullgray);
fill: var(--color-e-nearwhite);
height: 24px;
padding: 0 4px;
outline: none;
border: none;
display: inline-flex;
align-items: center;
&:hover {
background: var(--color-6-lowergray);
color: var(--color-f-white);
svg {
fill: var(--color-f-white);
}
}
&.active {
background: var(--color-accent);
fill: var(--color-f-white);
color: var(--color-f-white);
svg {
fill: var(--color-f-white);
}
}
& + button {
margin-left: 1px;
}
&:first-of-type {
border-radius: 2px 0 0 2px;
}
&:last-of-type {
border-radius: 0 2px 2px 0;
}
}
.icon-label,
.text-label {
display: inline-block;
vertical-align: top;
}
.text-label {
margin: 0 4px;
}
}
</style>
<script lang="ts">
import { defineComponent } from "vue";
import { defineComponent, PropType } from "vue";
import IconLabel from "@/components/widgets/labels/IconLabel.vue";
import TextLabel from "@/components/widgets/labels/TextLabel.vue";
export interface RadioEntryData {
value?: string;
label?: string;
icon?: string;
tooltip?: string;
action?: Function;
}
export type RadioEntries = Array<RadioEntryData>;
export default defineComponent({
components: {},
props: {
index: { type: Number, required: true },
},
data() {
return {
activeIndex: this.index,
};
},
mounted() {
this.updateActiveIconButton();
(this.$refs.radioInput as Element).querySelectorAll(".icon-button").forEach((iconButton, index) => {
iconButton.addEventListener("click", () => {
this.setActive(index);
});
});
},
watch: {
activeIndex() {
this.updateActiveIconButton();
},
entries: { type: Array as PropType<RadioEntries>, required: true },
selectedIndex: { type: Number, required: true },
},
methods: {
// This method may be called by the user of this component by setting a `ref="radioInput"` attribute and calling `(this.$refs.viewModePicker as typeof RadioInput).setActive(...)`
setActive(index: number) {
this.activeIndex = index;
this.$emit("update:index", index);
this.$emit("changed", index);
},
updateActiveIconButton() {
const iconButtons = (this.$refs.radioInput as Element).querySelectorAll(".icon-button");
iconButtons.forEach((iconButton) => iconButton.classList.remove("active"));
iconButtons[this.activeIndex].classList.add("active");
handleEntryClick(menuEntry: RadioEntryData) {
const index = this.entries.indexOf(menuEntry);
this.$emit("update:selectedIndex", index);
if (menuEntry.action) menuEntry.action();
},
},
components: {
IconLabel,
TextLabel,
},
});
</script>