package main import ( "context" "fmt" "net" "net/http" "net/http/httputil" "net/url" "os" "os/signal" "path/filepath" "strings" "syscall" "time" "github.com/gin-gonic/gin" "github.com/wailsapp/wails/v2" "github.com/wailsapp/wails/v2/pkg/menu" "github.com/wailsapp/wails/v2/pkg/menu/keys" "github.com/wailsapp/wails/v2/pkg/options" "github.com/wailsapp/wails/v2/pkg/options/assetserver" "github.com/wailsapp/wails/v2/pkg/options/mac" "github.com/Tencent/WeKnora/internal/config" "github.com/Tencent/WeKnora/internal/container" "github.com/Tencent/WeKnora/internal/logger" "github.com/Tencent/WeKnora/internal/runtime" "github.com/Tencent/WeKnora/internal/tracing" "github.com/Tencent/WeKnora/internal/types/interfaces" "github.com/joho/godotenv" wailsruntime "github.com/wailsapp/wails/v2/pkg/runtime" ) // dragHandlerJS is injected into the webview on DomReady. // It bypasses Wails' built-in CSS-variable-based drag detection (which uses // getComputedStyle and has timing/inheritance issues with dynamic SPA content) // and instead uses robust DOM-traversal via el.closest() plus a Y-position // fallback for the macOS title-bar region on layout containers. The "drag" // message is sent directly through the WKWebView script-message bridge, // which the native Objective-C handler in WailsContext.m converts to // [NSWindow performWindowDragWithEvent:]. const dragHandlerJS = `(function(){ if(window.__wkDragBound)return; window.__wkDragBound=true; if(window.wails&&window.wails.flags){ window.wails.flags.cssDragProperty='__disabled__'; window.wails.flags.cssDragValue='__never__'; } // Prevent native text/image drag-out to fix the "selected and dragged away" issue window.addEventListener('dragstart', function(e){ e.preventDefault(); }, true); var TITLEBAR_H=38; // We specifically look for Wails' inline style attributes injected by Vue, // and custom drag classes, avoiding generic headers like .section-header var dragSel='.logo_row,.menu_top,.drag-region,[data-wails-drag],' + '[style*="--wails-draggable: drag"],[style*="--wails-draggable:drag"]'; var noDragSel='button,a,input,select,textarea,[role="button"],' + '.t-button,.t-input,.t-select,.t-textarea,' + '.header-actions,.header-action-btn,.sidebar-toggle,.logo_box,' + '.close-btn,.menu_item,.submenu,.submenu_item,.menu_bottom,' + '.t-popup,.t-dropdown,.t-tooltip,.t-dialog,[data-no-drag],' + '[style*="--wails-draggable: no-drag"],[style*="--wails-draggable:no-drag"]'; var layoutClasses=['main','chat','dialogue-wrap','kb-list-container','kb-list-content', 'agent-list-container','agent-list-content','org-list-container','org-list-content','aside_box', 'ks-container','ks-content','settings-overlay','knowledge-layout', 'faq-manager-wrapper','login-layout']; function sendDrag(){ try{window.webkit.messageHandlers.external.postMessage('drag')} catch(_){try{window.WailsInvoke('drag')}catch(e){}} } function isLayoutEl(el){ for(var i=0;i= 0 { bundleName := filepath.Base(execPath[:idx+4]) if trimmed := strings.TrimSuffix(bundleName, ".app"); trimmed != "" { appName = trimmed } } return filepath.Join(homeDir, "Library", "Application Support", appName), nil } func resolveDesktopDataPath(rawPath, defaultRelativePath, appSupportDir string) string { trimmed := strings.TrimSpace(rawPath) if trimmed == "" { trimmed = defaultRelativePath } if filepath.IsAbs(trimmed) { return filepath.Clean(trimmed) } trimmed = strings.TrimPrefix(trimmed, "."+string(filepath.Separator)) return filepath.Join(appSupportDir, filepath.Clean(trimmed)) } func migrateLegacyDesktopData(resourcesDir, targetDataDir string) { legacyDataDir := filepath.Join(resourcesDir, "data") if info, err := os.Stat(legacyDataDir); err != nil || !info.IsDir() { return } if _, err := os.Stat(targetDataDir); err == nil { return } if err := os.MkdirAll(filepath.Dir(targetDataDir), 0o755); err != nil { logger.Warnf(context.Background(), "Failed to create app support parent dir %s: %v", filepath.Dir(targetDataDir), err) return } if err := os.Rename(legacyDataDir, targetDataDir); err != nil { logger.Warnf(context.Background(), "Failed to migrate legacy desktop data from %s to %s: %v", legacyDataDir, targetDataDir, err) return } logger.Infof(context.Background(), "Migrated legacy desktop data to %s", targetDataDir) } func listenWithRetry(addr string, maxRetries int, baseDelay time.Duration) (net.Listener, error) { var lastErr error for i := 0; i < maxRetries; i++ { listener, err := net.Listen("tcp", addr) if err == nil { return listener, nil } lastErr = err if i < maxRetries-1 { delay := baseDelay * time.Duration(1< 3*time.Second { delay = 3 * time.Second } time.Sleep(delay) } } return nil, lastErr }