feat: native directory picker for vault selection

This commit is contained in:
mirivlad 2026-06-17 09:13:09 +08:00
parent dd199f38ee
commit ffb3446cc3
5 changed files with 109 additions and 21 deletions

Binary file not shown.

View File

@ -16,41 +16,49 @@
appSettings = await App.GetAppSettings() || {}; appSettings = await App.GetAppSettings() || {};
recentVaults = appSettings.recentVaults || []; recentVaults = appSettings.recentVaults || [];
} catch (e) { } catch (e) {
// App settings might fail if backend not ready — show selection anyway
console.error('[VaultSelection] load settings:', e); console.error('[VaultSelection] load settings:', e);
} }
loading = false; loading = false;
}); });
async function browseNewVault() {
const path = await App.SelectDirectory();
if (path) {
newVaultPath = path;
}
}
async function browseOpenVault() {
const path = await App.SelectVaultForOpen();
if (path) {
openVaultPath = path;
}
}
async function createVault() { async function createVault() {
error = ''; error = '';
if (!newVaultPath.trim()) { if (!newVaultPath.trim()) {
error = 'Please enter a path for the new vault'; error = 'Please enter or select a path for the new vault';
return; return;
} }
creating = true; creating = true;
try { try {
// Step 1: Create the vault directory + metadata
const createErr = await App.CreateVault(newVaultPath.trim()); const createErr = await App.CreateVault(newVaultPath.trim());
if (createErr) { if (createErr) {
error = 'Create vault: ' + createErr; error = 'Create vault: ' + createErr;
creating = false; creating = false;
return; return;
} }
// Step 2: Open it (registers capabilities, loads plugin state)
const openErr = await App.OpenVault(newVaultPath.trim()); const openErr = await App.OpenVault(newVaultPath.trim());
if (openErr) { if (openErr) {
error = 'Open vault: ' + openErr; error = 'Open vault: ' + openErr;
creating = false; creating = false;
return; return;
} }
// Step 3: Save to app settings (set current + add to recent)
const setErr = await App.SetCurrentVault(newVaultPath.trim()); const setErr = await App.SetCurrentVault(newVaultPath.trim());
if (setErr) { if (setErr) {
// Vault is open but settings save failed — still proceed
console.warn('[VaultSelection] SetCurrentVault:', setErr); console.warn('[VaultSelection] SetCurrentVault:', setErr);
} }
// Success — notify app to transition to main UI
window.dispatchEvent(new CustomEvent('verstak:vault-opened')); window.dispatchEvent(new CustomEvent('verstak:vault-opened'));
} catch (e) { } catch (e) {
error = String(e); error = String(e);
@ -61,19 +69,17 @@
async function openExistingVault() { async function openExistingVault() {
error = ''; error = '';
if (!openVaultPath.trim()) { if (!openVaultPath.trim()) {
error = 'Please enter a path to an existing vault'; error = 'Please enter or select a path to an existing vault';
return; return;
} }
opening = true; opening = true;
try { try {
// Step 1: Open the vault
const openErr = await App.OpenVault(openVaultPath.trim()); const openErr = await App.OpenVault(openVaultPath.trim());
if (openErr) { if (openErr) {
error = 'Open vault: ' + openErr; error = 'Open vault: ' + openErr;
opening = false; opening = false;
return; return;
} }
// Step 2: Save to app settings
const setErr = await App.SetCurrentVault(openVaultPath.trim()); const setErr = await App.SetCurrentVault(openVaultPath.trim());
if (setErr) { if (setErr) {
console.warn('[VaultSelection] SetCurrentVault:', setErr); console.warn('[VaultSelection] SetCurrentVault:', setErr);
@ -141,9 +147,14 @@
<input <input
type="text" type="text"
bind:value={newVaultPath} bind:value={newVaultPath}
placeholder="~/Documents/MyVault" placeholder="Select or type a path..."
disabled={creating} disabled={creating}
/> />
<button class="btn-secondary" on:click={browseNewVault} type="button" disabled={creating}>
Browse…
</button>
</div>
<div class="button-row">
<button class="btn-primary" on:click={createVault} type="button" disabled={creating}> <button class="btn-primary" on:click={createVault} type="button" disabled={creating}>
{creating ? 'Creating...' : 'Create & Open'} {creating ? 'Creating...' : 'Create & Open'}
</button> </button>
@ -157,9 +168,14 @@
<input <input
type="text" type="text"
bind:value={openVaultPath} bind:value={openVaultPath}
placeholder="~/Documents/ExistingVault" placeholder="Select or type a path..."
disabled={opening} disabled={opening}
/> />
<button class="btn-secondary" on:click={browseOpenVault} type="button" disabled={opening}>
Browse…
</button>
</div>
<div class="button-row">
<button class="btn-primary" on:click={openExistingVault} type="button" disabled={opening}> <button class="btn-primary" on:click={openExistingVault} type="button" disabled={opening}>
{opening ? 'Opening...' : 'Open'} {opening ? 'Opening...' : 'Open'}
</button> </button>
@ -196,7 +212,7 @@
padding: 2rem; padding: 2rem;
} }
.vault-selection-inner { .vault-selection-inner {
max-width: 520px; max-width: 560px;
width: 100%; width: 100%;
} }
.loading-text { .loading-text {
@ -257,6 +273,7 @@
.input-row { .input-row {
display: flex; display: flex;
gap: 0.5rem; gap: 0.5rem;
margin-bottom: 0.5rem;
} }
.input-row input { .input-row input {
flex: 1; flex: 1;
@ -274,16 +291,19 @@
.input-row input::placeholder { .input-row input::placeholder {
color: #666; color: #666;
} }
.button-row {
display: flex;
justify-content: flex-end;
}
.btn-primary { .btn-primary {
background: #4ecca3; background: #4ecca3;
color: #1a1a2e; color: #1a1a2e;
border: none; border: none;
padding: 0.5rem 1rem; padding: 0.5rem 1.25rem;
border-radius: 6px; border-radius: 6px;
cursor: pointer; cursor: pointer;
font-size: 0.85rem; font-size: 0.85rem;
font-weight: 600; font-weight: 600;
white-space: nowrap;
} }
.btn-primary:hover:not(:disabled) { .btn-primary:hover:not(:disabled) {
background: #3dbb92; background: #3dbb92;
@ -292,6 +312,24 @@
opacity: 0.5; opacity: 0.5;
cursor: not-allowed; cursor: not-allowed;
} }
.btn-secondary {
background: #0f3460;
color: #a0a0b8;
border: 1px solid #1a3a5c;
padding: 0.5rem 0.75rem;
border-radius: 6px;
cursor: pointer;
font-size: 0.85rem;
white-space: nowrap;
}
.btn-secondary:hover:not(:disabled) {
background: #1a3a5c;
color: #e0e0f0;
}
.btn-secondary:disabled {
opacity: 0.5;
cursor: not-allowed;
}
.recent-section { .recent-section {
background: #16213e; background: #16213e;
border: 1px solid #0f3460; border: 1px solid #0f3460;

View File

@ -4,6 +4,7 @@ import {capability} from '../models';
import {api} from '../models'; import {api} from '../models';
import {permissions} from '../models'; import {permissions} from '../models';
import {plugin} from '../models'; import {plugin} from '../models';
import {context} from '../models';
export function CloseVault():Promise<void>; export function CloseVault():Promise<void>;
@ -39,9 +40,13 @@ export function RecordDesiredPlugin(arg1:string,arg2:string,arg3:string):Promise
export function ReloadPlugins():Promise<number|string>; export function ReloadPlugins():Promise<number|string>;
export function SelectDirectory():Promise<string>;
export function SelectVaultForOpen():Promise<string>;
export function SetCurrentVault(arg1:string):Promise<string>; export function SetCurrentVault(arg1:string):Promise<string>;
export function Startup():Promise<void>; export function Startup(arg1:context.Context):Promise<void>;
export function UpdateAppSettings(arg1:Record<string, any>):Promise<string>; export function UpdateAppSettings(arg1:Record<string, any>):Promise<string>;

View File

@ -70,12 +70,20 @@ export function ReloadPlugins() {
return window['go']['api']['App']['ReloadPlugins'](); return window['go']['api']['App']['ReloadPlugins']();
} }
export function SelectDirectory() {
return window['go']['api']['App']['SelectDirectory']();
}
export function SelectVaultForOpen() {
return window['go']['api']['App']['SelectVaultForOpen']();
}
export function SetCurrentVault(arg1) { export function SetCurrentVault(arg1) {
return window['go']['api']['App']['SetCurrentVault'](arg1); return window['go']['api']['App']['SetCurrentVault'](arg1);
} }
export function Startup() { export function Startup(arg1) {
return window['go']['api']['App']['Startup'](); return window['go']['api']['App']['Startup'](arg1);
} }
export function UpdateAppSettings(arg1) { export function UpdateAppSettings(arg1) {

View File

@ -2,12 +2,15 @@
package api package api
import ( import (
"context"
"fmt" "fmt"
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/wailsapp/wails/v2/pkg/runtime"
"github.com/verstak/verstak-desktop/internal/core/appsettings" "github.com/verstak/verstak-desktop/internal/core/appsettings"
"github.com/verstak/verstak-desktop/internal/core/capability" "github.com/verstak/verstak-desktop/internal/core/capability"
"github.com/verstak/verstak-desktop/internal/core/contribution" "github.com/verstak/verstak-desktop/internal/core/contribution"
@ -21,6 +24,7 @@ import (
// App is the main application struct exposed to the Wails frontend. // App is the main application struct exposed to the Wails frontend.
type App struct { type App struct {
ctx context.Context
capRegistry *capability.Registry capRegistry *capability.Registry
contribRegistry *contribution.Registry contribRegistry *contribution.Registry
permRegistry *permissions.Registry permRegistry *permissions.Registry
@ -57,10 +61,10 @@ func NewApp(
} }
} }
// Startup is called when the app starts. // Startup is called when the app starts. Sets the Wails context for dialogs.
func (a *App) Startup() error { func (a *App) Startup(ctx context.Context) {
a.ctx = ctx
log.Printf("[api] App.Startup: initialized with %d plugins", len(a.plugins)) log.Printf("[api] App.Startup: initialized with %d plugins", len(a.plugins))
return nil
} }
// ─── Plugin Manager API ───────────────────────────────────── // ─── Plugin Manager API ─────────────────────────────────────
@ -477,6 +481,39 @@ func (a *App) RecordDesiredPlugin(pluginID, version, source string) string {
return "" return ""
} }
// ─── Dialog API ─────────────────────────────────────────────
// SelectDirectory opens a native directory picker dialog.
// Returns the selected path or empty string if cancelled.
func (a *App) SelectDirectory() string {
home, _ := os.UserHomeDir()
selected, err := runtime.OpenDirectoryDialog(a.ctx, runtime.OpenDialogOptions{
Title: "Select Vault Directory",
DefaultDirectory: home,
})
if err != nil {
log.Printf("[api] SelectDirectory: %v", err)
return ""
}
return selected
}
// SelectVaultForOpen opens a directory picker for opening an existing vault.
func (a *App) SelectVaultForOpen() string {
home, _ := os.UserHomeDir()
selected, err := runtime.OpenDirectoryDialog(a.ctx, runtime.OpenDialogOptions{
Title: "Open Existing Vault",
DefaultDirectory: home,
})
if err != nil {
log.Printf("[api] SelectVaultForOpen: %v", err)
return ""
}
return selected
}
// ContributionSummary aggregates all contribution types for the frontend. // ContributionSummary aggregates all contribution types for the frontend.
type ContributionSummary struct { type ContributionSummary struct {
Views []contribution.ContributionView `json:"views"` Views []contribution.ContributionView `json:"views"`