diff --git a/@MyAppColors/MyAppColors.m b/@MyAppColors/MyAppColors.m index 55b7ce0..9cba18d 100644 --- a/@MyAppColors/MyAppColors.m +++ b/@MyAppColors/MyAppColors.m @@ -1,155 +1,156 @@ % Set of colors indended to introduce some uniformity in GUIs classdef (Abstract) MyAppColors methods (Static) %% Predefined colors % Colors are represented by rgb triplets returned by static methods function rgb = warning() rgb = [0.93, 0.69, 0.13]; % Orange end function rgb = error() rgb = [1,0,0]; % Red end % Labview-style lamp indicator colors function rgb = lampOn() rgb = [0,1,0]; % Bright green end function rgb = lampOff() rgb = [0,0.4,0]; % Dark green end % Recolor app according to a new color scheme function applyScheme(Obj, scheme) persistent init_default default_main_color ... default_label_text_color default_edit_text_color ... default_edit_field_color default_axes_label_color if ~exist('scheme', 'var') scheme = 'default'; end switch lower(scheme) case 'dark' main_color = [0.18, 0.19,0.57]; label_text_color = [1,1,1]; edit_text_color = [0,0,0]; edit_field_color = [1,1,1]; axes_label_color = [0.9,0.9,1]; case 'bright' main_color = [1,1,1]; label_text_color = [0,0,0.4]; edit_text_color = [0,0,0.]; edit_field_color = [1,1,1]; axes_label_color = [0,0,0]; case 'default' if isempty(init_default) % Create invisible components and read their % colors Uf = uifigure('Visible', false); Ef = uieditfield(Uf); Lbl = uilabel(Uf); Ax = axes(Uf); default_main_color = Uf.Color; default_label_text_color = Lbl.FontColor; default_edit_text_color = Ef.FontColor; default_edit_field_color = Ef.BackgroundColor; default_axes_label_color = Ax.XColor; delete(Uf); init_default = false; end main_color = default_main_color; label_text_color = default_label_text_color; edit_text_color = default_edit_text_color; edit_field_color = default_edit_field_color; axes_label_color = default_axes_label_color; otherwise error('Unknown scheme %s', scheme) end if isa(Obj, 'matlab.apps.AppBase') Fig = findFigure(Obj); MyAppColors.applyScheme(Fig, scheme); return end if ~isprop(Obj, 'Type') return end switch Obj.Type case 'figure' Obj.Color = main_color; case 'uitabgroup' % Nothing to do case 'uitab' Obj.ForegroundColor = edit_text_color; Obj.BackgroundColor = main_color; case 'uibutton' Obj.FontColor = label_text_color; Obj.BackgroundColor = main_color; case 'uistatebutton' Obj.FontColor = label_text_color; Obj.BackgroundColor = main_color; case 'uidropdown' Obj.FontColor = label_text_color; Obj.BackgroundColor = main_color; case 'uieditfield' Obj.FontColor = edit_text_color; Obj.BackgroundColor = edit_field_color; case {'uilabel', 'uicheckbox', 'uiradiobutton'} Obj.FontColor = label_text_color; case 'uilistbox' Obj.FontColor = edit_text_color; Obj.BackgroundColor = edit_field_color; case 'uitextarea' Obj.FontColor = edit_text_color; Obj.BackgroundColor = edit_field_color; case {'uipanel', 'uibuttongroup'} Obj.ForegroundColor = label_text_color; Obj.BackgroundColor = main_color; case 'axes' try % This property is only present in uiaxes Obj.BackgroundColor = main_color; catch end Obj.XColor = axes_label_color; Obj.YColor = axes_label_color; Obj.GridColor = [0.15, 0.15, 0.15]; + Obj.MinorGridColor = [0.15, 0.15, 0.15]; case 'uimenu' Obj.ForegroundColor = edit_text_color; case 'uicontrol' % The following switch case is for programmatically % created components switch Obj.Style case {'text', 'pushbutton', 'togglebutton'} Obj.ForegroundColor = label_text_color; Obj.BackgroundColor = main_color; case 'popupmenu' Obj.ForegroundColor = edit_text_color; Obj.BackgroundColor = edit_field_color; end end if isprop(Obj, 'Children') % Recolor children for i = 1:length(Obj.Children) MyAppColors.applyScheme(Obj.Children(i), scheme); end end end end end diff --git a/Utility functions/centerFigure.m b/Utility functions/centerFigure.m new file mode 100644 index 0000000..4375603 --- /dev/null +++ b/Utility functions/centerFigure.m @@ -0,0 +1,23 @@ +% Find figure within the object and move it to the center of the screen + +function centerFigure(Obj) + + % Find figure within the object if the object is not a figure itself + Fig = findFigure(Obj); + + if isempty(Fig) + return + end + + fig_w = Fig.Position(3); + fig_h = Fig.Position(4); + + R = groot(); + + % Display the figure window in the center of the screen + fig_x = R.ScreenSize(3)/2-fig_w/2; + fig_y = R.ScreenSize(4)/2-fig_h/2; + + Fig.Position(1:2) = [fig_x, fig_y]; +end + diff --git a/Utility functions/runSingletonApp.m b/Utility functions/runSingletonApp.m index fde652b..1085b20 100644 --- a/Utility functions/runSingletonApp.m +++ b/Utility functions/runSingletonApp.m @@ -1,36 +1,39 @@ function runSingletonApp(App, global_name) if isValidBaseVar(global_name) Fig = findFigure(App); close(Fig); % Make sure the new instance is deleted delete(App); App = evalin('base', global_name); % Bring to the focus the figure of existing app setFocus(App); error([global_name ' already exists']); else assignin('base', global_name, App); % Recolor app according to the present color scheme applyLocalColorScheme(App); + % Move the app figure to the center of the screen + centerFigure(App); + % Set up a listener that will clear the global name addlistener(App, 'ObjectBeingDestroyed', @clearGlobalName); end % The declaration of listener callback function clearGlobalName(~, ~) if ~isempty(global_name) try evalin('base', sprintf('clear(''%s'');', global_name)); catch ME warning(['Could not clear global variable ''' ... global_name '''. Error: ' ME.message]); end end end end