-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implemented dialogs via Zenity. Close #159
- Loading branch information
1 parent
1f33c6a
commit adb52eb
Showing
16 changed files
with
469 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
declare namespace Dialogs { | ||
type Providers = "Native" | "Zenity"; | ||
type Type = "error" | "warning" | "info" | "question"; | ||
|
||
interface SaveOptions { | ||
title?: string; | ||
defaultPath?: string; | ||
showsTagField?: boolean; | ||
} | ||
interface OpenOptions { | ||
buttonLabel?: string; | ||
defaultPath?: string; | ||
properties?: string[]; | ||
} | ||
|
||
type MessageBoxOptions = ErrorBoxOptions | QuestionBoxOptions | WarningBoxOptions; | ||
interface BaseBoxOptions { | ||
textOkButton?: string; | ||
title?: string; | ||
message: string; | ||
detail?: string; | ||
} | ||
interface ErrorBoxOptions extends BaseBoxOptions { | ||
type: "error"; | ||
} | ||
interface WarningBoxOptions extends BaseBoxOptions { | ||
type: "warning"; | ||
} | ||
interface QuestionBoxOptions extends BaseBoxOptions { | ||
type: "question"; | ||
textCancelButton?: string; | ||
defaultFocusedButton: "Ok" | "Cancel"; | ||
} | ||
} | ||
|
||
declare interface ProviderDialog { | ||
showMessageBox(params: Dialogs.MessageBoxOptions): Promise<number>; | ||
showMessageBoxSync(params: Dialogs.MessageBoxOptions): number; | ||
|
||
showOpenDialog(params: Dialogs.OpenOptions): Promise<string[] | null>; | ||
showOpenDialogSync(params: Dialogs.OpenOptions): string[] | null; | ||
|
||
showSaveDialog(params: Dialogs.SaveOptions): Promise<string | null>; | ||
showSaveDialogSync(params: Dialogs.SaveOptions): string | null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import * as E from "electron"; | ||
|
||
export class NativeDialogs implements ProviderDialog { | ||
constructor() {} | ||
|
||
public showMessageBox = async (options: Dialogs.MessageBoxOptions) => { | ||
const ops: E.MessageBoxOptions = { | ||
type: options.type, | ||
title: options.title, | ||
message: options.message, | ||
detail: options.detail, | ||
defaultId: 0, | ||
}; | ||
|
||
if (options.type === "question") { | ||
const buttons = ["Ok", "Cancel"]; | ||
|
||
if (options.textCancelButton) { | ||
buttons[1] = options.textCancelButton; | ||
} | ||
if (options.textOkButton) { | ||
buttons[0] = options.textOkButton; | ||
} | ||
ops.buttons = buttons; | ||
} else { | ||
if (options.textOkButton) { | ||
const buttons = ["Ok"]; | ||
|
||
buttons[0] = options.textOkButton; | ||
|
||
ops.buttons = buttons; | ||
} | ||
} | ||
|
||
const result = await E.dialog.showMessageBox(null, ops); | ||
return result.response; | ||
}; | ||
public showMessageBoxSync = (options: Dialogs.MessageBoxOptions) => { | ||
const ops: E.MessageBoxOptions = { | ||
type: options.type, | ||
title: options.title, | ||
message: options.message, | ||
detail: options.detail, | ||
defaultId: 0, | ||
}; | ||
|
||
if (options.type === "question") { | ||
const buttons = ["Ok", "Cancel"]; | ||
|
||
if (options.textCancelButton) { | ||
buttons[1] = options.textCancelButton; | ||
} | ||
if (options.textOkButton) { | ||
buttons[0] = options.textOkButton; | ||
} | ||
ops.buttons = buttons; | ||
} else { | ||
if (options.textOkButton) { | ||
const buttons = ["Ok"]; | ||
|
||
buttons[0] = options.textOkButton; | ||
|
||
ops.buttons = buttons; | ||
} | ||
} | ||
|
||
const result = E.dialog.showMessageBoxSync(null, ops); | ||
return result; | ||
}; | ||
|
||
public showOpenDialog = async (options: Dialogs.OpenOptions) => { | ||
const result = await E.dialog.showOpenDialog(null, options as E.OpenDialogOptions); | ||
return !result.canceled ? result.filePaths : null; | ||
}; | ||
public showOpenDialogSync = (options: Dialogs.OpenOptions) => { | ||
const result = E.dialog.showOpenDialogSync(null, options as E.OpenDialogOptions); | ||
return result || []; | ||
}; | ||
|
||
public showSaveDialog = async (options: Dialogs.SaveOptions) => { | ||
const result = await E.dialog.showSaveDialog(null, options); | ||
return !result.canceled && result.filePath ? result.filePath : null; | ||
}; | ||
public showSaveDialogSync = (options: Dialogs.SaveOptions) => { | ||
const result = E.dialog.showSaveDialogSync(null, options); | ||
return result; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
import { process } from "../Process"; | ||
|
||
export class ZenityDialogs implements ProviderDialog { | ||
constructor() {} | ||
|
||
public showMessageBox = async (options: Dialogs.MessageBoxOptions) => { | ||
const cmd = [`zenity --${options.type} --ellipsize`]; | ||
|
||
if (options.title) { | ||
cmd.push(`--title="${options.title}"`); | ||
} | ||
if (options.detail) { | ||
cmd.push(`--text="${options.message}\n${options.detail}"`); | ||
} | ||
if (options.textOkButton) { | ||
cmd.push(`--ok-label="${options.textOkButton}"`); | ||
} | ||
if (options.type === "question") { | ||
if (options.textCancelButton) { | ||
cmd.push(`--cancel-label="${options.textCancelButton}"`); | ||
} | ||
if (options.defaultFocusedButton === "Cancel") { | ||
cmd.push(`--default-cancel`); | ||
} | ||
} | ||
|
||
try { | ||
await process.exec(cmd.join(" ")); | ||
return 0; | ||
} catch (error) { | ||
return 1; | ||
} | ||
}; | ||
public showMessageBoxSync = (options: Dialogs.MessageBoxOptions) => { | ||
const cmd = [`zenity --${options.type} --ellipsize`]; | ||
|
||
if (options.title) { | ||
cmd.push(`--title="${options.title}"`); | ||
} | ||
if (options.detail) { | ||
cmd.push(`--text="${options.message}\n${options.detail}"`); | ||
} | ||
if (options.textOkButton) { | ||
cmd.push(`--ok-label="${options.textOkButton}"`); | ||
} | ||
if (options.type === "question") { | ||
if (options.textCancelButton) { | ||
cmd.push(`--cancel-label="${options.textCancelButton}"`); | ||
} | ||
if (options.defaultFocusedButton === "Cancel") { | ||
cmd.push(`--default-cancel`); | ||
} | ||
} | ||
|
||
try { | ||
process.execSync(cmd.join(" ")); | ||
return 0; | ||
} catch (error) { | ||
return 1; | ||
} | ||
}; | ||
|
||
public showOpenDialog = async (options: Dialogs.OpenOptions) => { | ||
const cmd = ["zenity --file-selection"]; | ||
|
||
if (options.defaultPath) { | ||
cmd.push(`--filename="${options.defaultPath}"`); | ||
} | ||
if (Array.isArray(options.properties) && options.properties.length > 0) { | ||
for (const prop of options.properties) { | ||
switch (prop) { | ||
case "openDirectory": { | ||
cmd.push(`--directory`); | ||
break; | ||
} | ||
case "multiSelections": { | ||
cmd.push(`--multiple`); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
let result: string[] | undefined; | ||
try { | ||
const stdout = await process.exec(cmd.join(" ")); | ||
result = stdout.replace(/\n/, "").split("|"); | ||
} catch (error) { | ||
return null; | ||
} | ||
|
||
return result; | ||
}; | ||
public showOpenDialogSync = (options: Dialogs.OpenOptions) => { | ||
const cmd = ["zenity --file-selection"]; | ||
|
||
if (options.defaultPath) { | ||
cmd.push(`--filename="${options.defaultPath}"`); | ||
} | ||
if (Array.isArray(options.properties) && options.properties.length > 0) { | ||
for (const prop of options.properties) { | ||
switch (prop) { | ||
case "openDirectory": { | ||
cmd.push(`--directory`); | ||
break; | ||
} | ||
case "multiSelections": { | ||
cmd.push(`--multiple`); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
let result: string[] | undefined; | ||
try { | ||
const stdout = process.execSync(cmd.join(" ")); | ||
result = stdout.replace(/\n/, "").split("|"); | ||
} catch (error) { | ||
return null; | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
public showSaveDialog = async (options: Dialogs.SaveOptions) => { | ||
const cmd = ["zenity --file-selection --save --confirm-overwrite"]; | ||
|
||
if (options.defaultPath) { | ||
cmd.push(`--filename="${options.defaultPath}"`); | ||
} | ||
|
||
let result: string | undefined; | ||
try { | ||
result = await process.exec(cmd.join(" ")); | ||
result = result.replace(/\n/, ""); | ||
} catch (error) { | ||
return null; | ||
} | ||
|
||
return result; | ||
}; | ||
public showSaveDialogSync = (options: Dialogs.SaveOptions) => { | ||
const cmd = ["zenity --file-selection --save --confirm-overwrite"]; | ||
|
||
if (options.defaultPath) { | ||
cmd.push(`--filename="${options.defaultPath}"`); | ||
} | ||
|
||
let result: string | undefined; | ||
try { | ||
result = process.execSync(cmd.join(" ")); | ||
result = result.replace(/\n/, ""); | ||
} catch (error) { | ||
return null; | ||
} | ||
|
||
return result; | ||
}; | ||
} |
Oops, something went wrong.