Skip to content

Instantly share code, notes, and snippets.

@freeonterminate
Created June 19, 2015 03:08
Show Gist options
  • Save freeonterminate/a01e750045f8da43a61b to your computer and use it in GitHub Desktop.
Save freeonterminate/a01e750045f8da43a61b to your computer and use it in GitHub Desktop.
TTabControl で先頭のタブをプログラムで変更可能にするヘルパー
unit uTabControlHelper;
interface
uses
FMX.TabControl;
type
TTabControlHelper = class helper for TTabControl
private
function GetFirstTab: TTabItem;
function GetFirstTabIndex: Integer;
procedure SetFirstTab(const Value: TTabItem);
procedure SetFirstTabIndex(const Value: Integer);
function GetVisibleTabCount: Integer;
public
property FirstTabIndex: Integer read GetFirstTabIndex write SetFirstTabIndex;
property FirstTab: TTabItem read GetFirstTab write SetFirstTab;
property VisibleTabCount: Integer read GetVisibleTabCount;
end;
implementation
uses
System.Generics.Collections
;
{ TTabControlHelper }
var
GFirstTabIndex: Integer = 0;
GTabVisibles: TDictionary<TTabItem, Boolean>;
function TTabControlHelper.GetFirstTab: TTabItem;
begin
Result := Tabs[GFirstTabIndex]
end;
function TTabControlHelper.GetFirstTabIndex: Integer;
begin
Result := GFirstTabIndex;
end;
function TTabControlHelper.GetVisibleTabCount: Integer;
var
W: Single;
i: Integer;
Tab: TTabItem;
begin
W := Width;
i := GFirstTabIndex;
while (True) do
begin
Tab := Tabs[i];
if (Tab = nil) or (W < Tab.Width) then
Break;
if (Tab.Visible) then
W := W - Tabs[i].Width;
Inc(i);
end;
Result := i - GFirstTabIndex;
end;
procedure TTabControlHelper.SetFirstTab(const Value: TTabItem);
var
i, j: Integer;
Tab: TTabItem;
TabVisible: TPair<TTabItem, Boolean>;
begin
if (Value = nil) then
Exit;
BeginUpdate;
try
for TabVisible in GTabVisibles do
TabVisible.Key.Visible := TabVisible.Value;
GTabVisibles.Clear;
for i := 0 to TabCount - 1 do
if (Tabs[i] = Value) then
begin
for j := 0 to i - 1 do
begin
Tab := Tabs[j];
GTabVisibles.AddOrSetValue(Tab, Tab.Visible);
Tab.Visible := False;
end;
Value.Visible := True;
GFirstTabIndex := Value.Index;
Break;
end;
FScene.AddUpdateRect(Self.BoundsRect);
finally
EndUpdate;
end;
end;
procedure TTabControlHelper.SetFirstTabIndex(const Value: Integer);
begin
SetFirstTab(Tabs[Value]);
end;
initialization
GTabVisibles := TDictionary<TTabItem, Boolean>.Create;
finalization
GTabVisibles.DisposeOf;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment