Skip to content

Commit 3bd9e4c

Browse files
Added mechanism to restore the contextual tabs, when the Window handle is recreated. When a new handle is created, all commands need to be recreated as well.
Information such as the availability of contextual tabs is part of specific commands and is lost when the commands are destroyed.
1 parent dfee965 commit 3bd9e4c

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

Lib/UIRibbon.pas

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ TUIRibbon = class(TWinControl, IUIApplication)
216216
///These members are used to register for changes within the action manager's image lists. See procedure RegisterForImageChanges.
217217
fImageChangeLink: TChangeLink;
218218
fLargeImageChangeLink: TChangeLink;
219+
/// When the window handle is destroyed, the commands are as well. When recreating the window handle, we need to keep track of available context tabs, so that we can restore them
220+
fStoredContextTabAvailability: TDictionary<Integer, TUIContextAvailability>;
219221
{ IUIApplication }
220222
function OnViewChanged(ViewId: UInt32; TypeId: _UIViewType;
221223
const View: IUnknown; Verb: _UIViewVerb; ReasonCode: Int32): HRESULT; stdcall;
@@ -236,6 +238,8 @@ TUIRibbon = class(TWinControl, IUIApplication)
236238
procedure LoadFramework();
237239
/// Called during the initialization of the framework. We register a change event handler towards the action manager's image list (in case that roAssignImagesFromActionManager is used)
238240
procedure RegisterForImageChanges;
241+
procedure StoreContextTabAvailability;
242+
procedure RestoreContextTabAvailability;
239243
protected
240244
procedure CreateWnd(); override;
241245
procedure DestroyWnd(); override;
@@ -682,6 +686,7 @@ constructor TUIRibbon.Create(AOwner: TComponent);
682686
FResourceName := 'APPLICATION';
683687
FCommands := TObjectDictionary<Cardinal, TUICommand>.Create([doOwnsValues]);
684688
fUseDarkMode := TDarkMode.Never;
689+
fStoredContextTabAvailability := TDictionary<Integer, TUIContextAvailability>.Create;
685690

686691
// Ensure the control is used on a TCustomForm.
687692
ParentForm := GetParentForm(AOwner as TWinControl);
@@ -739,6 +744,7 @@ destructor TUIRibbon.Destroy;
739744
begin
740745
FreeAndNil(fImageChangeLink);
741746
FreeAndNil(fLargeImageChangeLink);
747+
FreeAndNil(fStoredContextTabAvailability);
742748
if roAutoPreserveState in Options then
743749
SaveRibbonSettings(); // Save quick toolbar, etc.
744750

@@ -1248,11 +1254,16 @@ procedure TUIRibbon.CreateWnd;
12481254
begin
12491255
inherited;
12501256
Load;
1257+
// Check if there are contextual tabs stored that need to be re-activated
1258+
if fStoredContextTabAvailability.Count > 0 then
1259+
RestoreContextTabAvailability;
12511260
end;
12521261

12531262
procedure TUIRibbon.DestroyWnd;
12541263
begin
12551264
inherited;
1265+
if (csRecreating in ControlState) then
1266+
StoreContextTabAvailability;
12561267
if Assigned(FFramework) and FAvailable then
12571268
FFramework.Destroy;
12581269
FFramework := nil;
@@ -1263,6 +1274,30 @@ procedure TUIRibbon.DestroyWnd;
12631274
fLoaded := False;
12641275
end;
12651276

1277+
procedure TUIRibbon.StoreContextTabAvailability;
1278+
var
1279+
lCommand: TUICommand;
1280+
begin
1281+
for lCommand in fCommands.Values do
1282+
begin
1283+
if lCommand.CommandType = TUICommandType.ctContext then
1284+
fStoredContextTabAvailability.Add(lCommand.CommandId, (lCommand as TUICommandContext).Availability);
1285+
end;
1286+
end;
1287+
1288+
procedure TUIRibbon.RestoreContextTabAvailability;
1289+
var
1290+
lCommandID: Integer;
1291+
lCommand: TUICommand;
1292+
begin
1293+
for lCommandID in fStoredContextTabAvailability.Keys do
1294+
begin
1295+
if Self.TryGetCommand(lCommandId, lCommand) then
1296+
(lCommand as TUICommandContext).Availability := fStoredContextTabAvailability[lCommandId]
1297+
end;
1298+
fStoredContextTabAvailability.Clear;
1299+
end;
1300+
12661301
function TUIRibbon.LoadRibbonSettings(): boolean;
12671302
var
12681303
lSettingsFileFullPath: string;

0 commit comments

Comments
 (0)