Skip to content

Commit

Permalink
Code cleanup, fix #13, fix #20, fix #18, fix #17
Browse files Browse the repository at this point in the history
  • Loading branch information
gnunn1 committed Jan 14, 2016
1 parent 3f3adb4 commit a85f525
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 70 deletions.
2 changes: 1 addition & 1 deletion dub.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"copyright": "Copyright © 2015, Gerald Nunn",
"authors": ["Gerald Nunn"],
"dependencies": {
"gtk-d": "~>3.2.1"
"gtk-d": "3.2.1"
},
"buildTypes": {
"release": {
Expand Down
46 changes: 23 additions & 23 deletions source/gx/terminix/colorschemes.d
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,23 @@ enum SCHEME_KEY_USE_THEME_COLORS = "use-theme-colors";
* and palette colors similar to what text editor color schemes typically
* do.
*/
struct ColorScheme {
class ColorScheme {
string id;
string name;
string comment;
bool useThemeColors;
RGBA foreground;
RGBA background;
RGBA[16] palette;

bool opEquals(ColorScheme c) {
return (id == c.id && name == c.name && comment == c.comment && useThemeColors == c.useThemeColors && equal(foreground, c.foreground) && equal(background,
c.background) && palette == palette);
}

this() {
id = randomUUID().toString();
foreground = new RGBA();
background = new RGBA();
for (int i=0; i<16; i++) {
palette[i] = new RGBA();
}
}
}

/**
Expand Down Expand Up @@ -116,37 +120,33 @@ ColorScheme[] loadColorSchemes() {
* Loads a color scheme from a JSON file
*/
private ColorScheme loadScheme(string fileName) {
ColorScheme cs = new ColorScheme();

string content = readText(fileName);
JSONValue root = parseJSON(content);
string name = root[SCHEME_KEY_NAME].str();
string comment;
cs.name = root[SCHEME_KEY_NAME].str();
if (SCHEME_KEY_COMMENT in root) {
comment = root[SCHEME_KEY_COMMENT].str();
cs.comment = root[SCHEME_KEY_COMMENT].str();
}
bool useThemeColors = root[SCHEME_KEY_USE_THEME_COLORS].type == JSON_TYPE.TRUE ? true : false;
RGBA foreground;
cs.useThemeColors = root[SCHEME_KEY_USE_THEME_COLORS].type == JSON_TYPE.TRUE ? true : false;
if (SCHEME_KEY_FOREGROUND in root) {
foreground = getColor(root[SCHEME_KEY_FOREGROUND].str());
parseColor(cs.foreground, root[SCHEME_KEY_FOREGROUND].str());
}
RGBA background;
if (SCHEME_KEY_BACKGROUND in root) {
background = getColor(root[SCHEME_KEY_BACKGROUND].str());
parseColor(cs.background, root[SCHEME_KEY_BACKGROUND].str());
}
JSONValue[] rawPalette = root[SCHEME_KEY_PALETTE].array();
if (rawPalette.length != 16) {
throw new Exception(_("Color scheme palette requires 16 colors"));
}
RGBA[16] palette;
foreach (i, value; rawPalette) {
palette[i] = getColor(value.str());
parseColor(cs.palette[i], value.str());
}
return ColorScheme(randomUUID().toString(), name, comment, useThemeColors, foreground, background, palette);
return cs;
}

private RGBA getColor(string value) {
if (value is null)
return null;
RGBA color = new RGBA();
color.parse(value);
return color;
private void parseColor(RGBA rgba, string value) {
if (value.length == 0)
return;
rgba.parse(value);
}
6 changes: 2 additions & 4 deletions source/gx/terminix/prefwindow.d
Original file line number Diff line number Diff line change
Expand Up @@ -448,10 +448,9 @@ private:
setMarginRight(18);

int row = 0;
Label lblBehavior = new Label("");
Label lblBehavior = new Label(format("<b>%s</b>", _("Behavior")));
lblBehavior.setUseMarkup(true);
lblBehavior.setHalign(Align.START);
lblBehavior.setMarkup(format("<b>%s</b>", _("Behavior")));
attach(lblBehavior, 0, row, 2, 1);
row++;

Expand All @@ -469,10 +468,9 @@ private:
row++;
}

Label lblAppearance = new Label("");
Label lblAppearance = new Label(format("<b>%s</b>", _("Appearance")));
lblAppearance.setUseMarkup(true);
lblAppearance.setHalign(Align.START);
lblAppearance.setMarkup(format("<b>%s</b>", _("Appearance")));
attach(lblAppearance, 0, row, 2, 1);
row++;

Expand Down
27 changes: 13 additions & 14 deletions source/gx/terminix/profilewindow.d
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ private:
//Text Appearance
Box b = new Box(Orientation.VERTICAL, 6);
b.setMarginTop(18);
Label lblTitle = new Label("");
lblTitle.setMarkup(_("<b>Text Appearance</b>"));
Label lblTitle = new Label(_("<b>Text Appearance</b>"));
lblTitle.setUseMarkup(true);
lblTitle.setHalign(Align.START);
b.add(lblTitle);

Expand Down Expand Up @@ -265,8 +265,8 @@ private:
grid.setRowSpacing(18);

int row = 0;
Label lblScheme = new Label("");
lblScheme.setMarkup(_("<b>Color scheme</b>"));
Label lblScheme = new Label(_("<b>Color scheme</b>"));
lblScheme.setUseMarkup(true);
lblScheme.setHalign(Align.END);
grid.attach(lblScheme, 0, row, 1, 1);

Expand All @@ -286,16 +286,16 @@ private:
grid.attach(cbScheme, 1, row, 1, 1);
row++;

Label lblPalette = new Label("");
lblPalette.setMarkup(_("<b>Color palette</b>"));
Label lblPalette = new Label(_("<b>Color palette</b>"));
lblPalette.setUseMarkup(true);
lblPalette.setHalign(Align.END);
lblPalette.setValign(Align.START);
grid.attach(lblPalette, 0, row, 1, 1);
grid.attach(createColorGrid(row), 1, row, 1, 1);
row++;

Label lblOptions = new Label("");
lblOptions.setMarkup(_("<b>Options</b>"));
Label lblOptions = new Label(_("<b>Options</b>"));
lblOptions.setUseMarkup(true);
lblOptions.setValign(Align.START);
lblOptions.setHalign(Align.END);
grid.attach(lblOptions, 0, row, 1, 1);
Expand Down Expand Up @@ -355,7 +355,7 @@ private:
gsProfile.setString(SETTINGS_PROFILE_FG_COLOR_KEY, rgbaTo16bitHex(color, false, true));
});

Label lblSpacer = new Label("");
Label lblSpacer = new Label(" ");
lblSpacer.setHexpand(true);
gColors.attach(lblSpacer, 3, row, 1, 1);

Expand Down Expand Up @@ -395,7 +395,7 @@ private:

void onPaletteColorSet(ColorButton cb) {
setCustomScheme();
RGBA color = new RGBA();
RGBA color;
cb.getRgba(color);
string[] colorValues = gsProfile.getStrv(SETTINGS_PROFILE_PALETTE_COLOR_KEY);
colorValues[cast(int) cb.getData(PALETTE_COLOR_INDEX_KEY)] = rgbaTo16bitHex(color, false, true);
Expand All @@ -412,11 +412,10 @@ private:
//Initialize ColorScheme combobox
RGBA[16] colors;
foreach (i, cb; cbPalette) {
colors[i] = new RGBA();
cb.getRgba(colors[i]);
}
RGBA fg = new RGBA();
RGBA bg = new RGBA();
RGBA fg;
RGBA bg;
cbFG.getRgba(fg);
cbBG.getRgba(bg);
int index = findSchemeByColors(schemes, cbUseThemeColors.getActive(), fg, bg, colors);
Expand Down Expand Up @@ -618,7 +617,7 @@ private:
add(bCommand);

Box bWhenExits = new Box(Orientation.HORIZONTAL, 12);
Label lblWhenExists = new Label("When command exists");
Label lblWhenExists = new Label(_("When command exists"));
bWhenExits.add(lblWhenExists);
ComboBox cbWhenExists = createNameValueCombo([_("Exit the terminal"), _("Restart the command"), _("Hold the terminal open")], SETTINGS_PROFILE_EXIT_ACTION_VALUES);
gsProfile.bind(SETTINGS_PROFILE_EXIT_ACTION_KEY, cbWhenExists, "active-id", GSettingsBindFlags.DEFAULT);
Expand Down
58 changes: 30 additions & 28 deletions source/gx/terminix/terminal/terminal.d
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,10 @@ private:
*/
void updateTitle() {
string title = overrideTitle is null ? gsProfile.getString(SETTINGS_PROFILE_TITLE_KEY) : overrideTitle;
title = title.replace(TERMINAL_TITLE, vte.getWindowTitle());
string windowTitle = vte.getWindowTitle();
if (windowTitle.length == 0)
windowTitle = _("Terminal");
title = title.replace(TERMINAL_TITLE, windowTitle);
title = title.replace(TERMINAL_ICON_TITLE, vte.getIconTitle());
title = title.replace(TERMINAL_ID, to!string(terminalID));
string path;
Expand All @@ -498,8 +501,6 @@ private:
path = "";
}
title = title.replace(TERMINAL_DIR, path);
if (title.length == 0)
title = _("Terminal");
lblTitle.setMarkup(title);
}

Expand Down Expand Up @@ -600,6 +601,21 @@ private:
lblTitle.setSensitive(false);
return false;
}

// Preferences go here
private:
RGBA fg;
RGBA bg;
RGBA[16] palette;

void initColors() {
fg = new RGBA();
bg = new RGBA();
palette = new RGBA[16];
for (int i=0; i<16; i++) {
palette[i] = new RGBA();
}
}

/**
* Updates a setting based on the passed key. Note that using gio.Settings.bind
Expand All @@ -621,43 +637,24 @@ private:
vte.setCursorShape(getCursorShape(gsProfile.getString(SETTINGS_PROFILE_CURSOR_SHAPE_KEY)));
break;
case SETTINGS_PROFILE_FG_COLOR_KEY, SETTINGS_PROFILE_BG_COLOR_KEY, SETTINGS_PROFILE_PALETTE_COLOR_KEY,
SETTINGS_PROFILE_USE_THEME_COLORS_KEY:
RGBA fg;
RGBA bg;
SETTINGS_PROFILE_USE_THEME_COLORS_KEY, SETTINGS_PROFILE_BG_TRANSPARENCY_KEY:
if (gsProfile.getBoolean(SETTINGS_PROFILE_USE_THEME_COLORS_KEY)) {
trace("using theme colors");
vte.getStyleContext().getColor(StateFlags.ACTIVE, fg);
vte.getStyleContext().getBackgroundColor(StateFlags.ACTIVE, bg);
} else {
fg = new RGBA();
bg = new RGBA();
if (!fg.parse(gsProfile.getString(SETTINGS_PROFILE_FG_COLOR_KEY)))
trace("Parsing foreground color failed");
if (!bg.parse(gsProfile.getString(SETTINGS_PROFILE_BG_COLOR_KEY)))
trace("Parsing background color failed");
}
double alpha = to!double(100 - gsProfile.getInt(SETTINGS_PROFILE_BG_TRANSPARENCY_KEY)) / 100.0;
bg.alpha = alpha;
bg.alpha = to!double(100 - gsProfile.getInt(SETTINGS_PROFILE_BG_TRANSPARENCY_KEY)) / 100.0;
string[] colors = gsProfile.getStrv(SETTINGS_PROFILE_PALETTE_COLOR_KEY);
RGBA[] palette = new RGBA[colors.length];
foreach (i, color; colors) {
palette[i] = new RGBA();
if (!palette[i].parse(colors[i])) trace("Parsing color failed " ~ colors[i]);
if (!palette[i].parse(color)) trace("Parsing color failed " ~ colors[i]);
}
vte.setColors(fg, bg, palette);
break;
case SETTINGS_PROFILE_BG_TRANSPARENCY_KEY:
RGBA bg;
if (gsProfile.getBoolean(SETTINGS_PROFILE_USE_THEME_COLORS_KEY)) {
vte.getStyleContext().getBackgroundColor(StateFlags.ACTIVE, bg);
} else {
bg = new RGBA();
if (!bg.parse(gsProfile.getString(SETTINGS_PROFILE_BG_COLOR_KEY)))
trace("Parsing background color failed");
}
double alpha = to!double(100 - gsProfile.getInt(SETTINGS_PROFILE_BG_TRANSPARENCY_KEY)) / 100.0;
bg.alpha = alpha;
vte.setColorBackground(bg);
break;
case SETTINGS_PROFILE_SHOW_SCROLLBAR_KEY:
sb.setVisible(gsProfile.getBoolean(SETTINGS_PROFILE_SHOW_SCROLLBAR_KEY));
break;
Expand Down Expand Up @@ -733,6 +730,8 @@ private:
}
}

private:

/**
* Spawns the child process in the Terminal depending on the Profile
* command options.
Expand All @@ -749,9 +748,10 @@ private:
if (gsProfile.getBoolean(SETTINGS_PROFILE_LOGIN_SHELL_KEY)) {
args ~= "-" ~ shell;
}
flags = GSpawnFlags.FILE_AND_ARGV_ZERO;
flags = GSpawnFlags.DEFAULT;
}
string[] envv = [""];
foreach(arg; args) trace("Argument: " ~ arg);
vte.spawnSync(VtePtyFlags.DEFAULT, initialPath, args, envv, flags, null, null, gpid, null);
vte.grabFocus();
}
Expand Down Expand Up @@ -1028,6 +1028,7 @@ public:
*/
this(string profileUUID) {
super(Orientation.VERTICAL, 0);
initColors();
_terminalUUID = randomUUID().toString();
_profileUUID = profileUUID;
gsProfile = prfMgr.getProfileSettings(profileUUID);
Expand All @@ -1041,7 +1042,7 @@ public:
}

/**
* initializes the terminal, i.w spawns the child process.
* initializes the terminal, i.e spawns the child process.
*
* Params:
* initialPath = The initial working directory for the terminal
Expand All @@ -1054,6 +1055,7 @@ public:
if (firstRun) {
vte.setSize(gsProfile.getInt(SETTINGS_PROFILE_SIZE_COLUMNS_KEY), gsProfile.getInt(SETTINGS_PROFILE_SIZE_ROWS_KEY));
}
updateTitle();
}

/**
Expand Down

0 comments on commit a85f525

Please sign in to comment.