IDE: Shorten filenames in Recent Files/Projects/Packages menus. Issue #10297.

git-svn-id: trunk@56047 -
This commit is contained in:
juha 2017-10-13 13:00:32 +00:00
parent d15f1f3524
commit c6219aa9d6
6 changed files with 53 additions and 13 deletions

View File

@ -2466,8 +2466,7 @@ begin
RemoveFromRecentList(AFilename,FRecentOpenFiles,rltFile);
end;
procedure TEnvironmentOptions.RemoveFromRecentPackageFiles(
const AFilename: string);
procedure TEnvironmentOptions.RemoveFromRecentPackageFiles(const AFilename: string);
begin
RemoveFromRecentList(AFilename,FRecentPackageFiles,rltFile);
end;

View File

@ -108,6 +108,7 @@ function SwitchPathDelims(const Filename: string; Switch: boolean): string;
function CheckPathDelim(const OldPathDelim: string; out Changed: boolean): TPathDelimSwitch;
function IsCurrentPathDelim(Switch: TPathDelimSwitch): boolean;
function ChompEndNumber(const s: string): string;
function ShortDisplayFilename(const aFileName: string): string;
// cmd line
procedure SplitCmdLine(const CmdLine: string;
@ -923,6 +924,35 @@ begin
Result:=copy(Result,1,NewLen);
end;
function ShortDisplayFilename(const aFileName: string): string;
// Shorten a long filename for display.
// Add '...' after the 2. path delimiter, then the end part of filename.
const
Limit = 80;
var
StartLen, EndLen, SepCnt: Integer;
begin
if Length(aFileName) > Limit then
begin
StartLen := 1;
SepCnt := 0;
while StartLen < Length(aFileName) - (Limit div 2) do
begin
if aFileName[StartLen] in AllowDirectorySeparators then
begin
Inc(SepCnt);
if SepCnt = 2 then Break;
end;
Inc(StartLen);
end;
EndLen := Limit - StartLen - 3;
Result := Copy(aFileName, 1, StartLen) + '...'
+ Copy(aFileName, Length(aFileName)-EndLen+1, EndLen);
end
else
Result := aFileName;
end;
function FindFirstFileWithExt(const Directory, Ext: string): string;
var
FileInfo: TSearchRec;

View File

@ -2995,7 +2995,8 @@ procedure TMainIDE.mnuOpenRecentClicked(Sender: TObject);
var
AFilename: string;
begin
AFileName:=ExpandFileNameUTF8((Sender as TIDEMenuItem).Caption);
// Hint holds the full filename, Caption may have a shortened form.
AFileName:=(Sender as TIDEMenuItem).Hint;
if DoOpenEditorFile(AFilename,-1,-1,[ofAddToRecent])=mrOk then begin
UpdateEnvironment;
end else begin

View File

@ -516,7 +516,7 @@ end;
procedure TOpenFileToolButton.mnuOpenFile(Sender: TObject);
begin
if MainIDE.DoOpenEditorFile((Sender as TOpenFileMenuItem).FileName,-1,-1,
if MainIDE.DoOpenEditorFile((Sender as TOpenFileMenuItem).Hint, -1, -1,
[ofAddToRecent])=mrOk then
begin
MainIDE.SetRecentFilesMenu;
@ -526,10 +526,12 @@ end;
procedure TOpenFileToolButton.mnuProjectFile(Sender: TObject);
begin
MainIDE.DoOpenProjectFile((Sender as TOpenFileMenuItem).FileName,[ofAddToRecent]);
// Hint holds the full filename, Caption may have a shortened form.
MainIDE.DoOpenProjectFile((Sender as TOpenFileMenuItem).Hint, [ofAddToRecent]);
end;
procedure TOpenFileToolButton.RefreshMenu(Sender: TObject);
procedure AddFile(const AFileName: string; const AOnClick: TNotifyEvent);
var
AMenuItem: TOpenFileMenuItem;
@ -539,7 +541,8 @@ procedure TOpenFileToolButton.RefreshMenu(Sender: TObject);
DropdownMenu.Items.Add(AMenuItem);
AMenuItem.OnClick := AOnClick;
AMenuItem.FileName := AFileName;
AMenuItem.Caption := AFilename;
AMenuItem.Caption := ShortDisplayFilename(AFilename);
AMenuItem.Hint := AFilename; // Hint is not shown, it just holds the full filename.
xExt := ExtractFileExt(AFileName);
if SameFileName(xExt, '.lpi') or SameFileName(xExt, '.lpr') then
AMenuItem.ImageIndex := LoadProjectIconIntoImages(AFileName, DropdownMenu.Images, FIndex);
@ -1933,7 +1936,8 @@ begin
// set captions and event
for i:=0 to FileList.Count-1 do begin
AMenuItem:=Section.Items[i];
AMenuItem.Caption := FileList[i];
AMenuItem.Caption := ShortDisplayFilename(FileList[i]);
AMenuItem.Hint := FileList[i]; // Hint is not shown, it just holds the full filename.
AMenuItem.OnClick := OnClickEvent;
end;
end;

View File

@ -4361,15 +4361,19 @@ end;
procedure TLazSourceFileManager.OpenProject(aMenuItem: TIDEMenuItem);
var
OpenDialog:TOpenDialog;
OpenDialog: TOpenDialog;
AFileName: string;
LoadFlags: TLoadBufferFlags;
PreReadBuf: TCodeBuffer;
SourceType: String;
LPIFilename: String;
begin
if Assigned(aMenuItem) and (aMenuItem.Section=itmProjectRecentOpen) then begin
AFileName:=ExpandFileNameUTF8(aMenuItem.Caption);
if Assigned(aMenuItem) and (aMenuItem.Section=itmProjectRecentOpen) then
begin
// Hint holds the full filename, Caption may have a shortened form.
AFileName:=aMenuItem.Hint;
Assert(AFileName = ExpandFileNameUTF8(AFileName),
'TLazSourceFileManager.OpenProject: AFileName is not absolute.');
if MainIDE.DoOpenProjectFile(AFilename,[ofAddToRecent])=mrOk then begin
AddRecentProjectFile(AFilename);
end else begin

View File

@ -1065,14 +1065,15 @@ procedure TPkgManager.MainIDEitmOpenRecentPackageClicked(Sender: TObject);
var
AFilename: string;
begin
AFileName:=ExpandFileNameUTF8((Sender as TIDEMenuItem).Caption);
// Hint holds the full filename, Caption may have a shortened form.
AFileName:=(Sender as TIDEMenuItem).Hint;
if DoOpenPackageFile(AFilename,[pofAddToRecent],false)=mrOk then begin
UpdateEnvironment;
end else begin
// open failed
if not FileExistsUTF8(AFilename) then begin
// file does not exist -> delete it from recent file list
RemoveFromRecentList(AFilename,EnvironmentOptions.RecentPackageFiles,rltFile);
EnvironmentOptions.RemoveFromRecentPackageFiles(AFilename);
UpdateEnvironment;
end;
end;
@ -3034,7 +3035,8 @@ end;
procedure TPkgManager.SetRecentPackagesMenu;
begin
MainIDE.SetRecentSubMenu(itmPkgOpenRecent,
EnvironmentOptions.RecentPackageFiles,@MainIDEitmOpenRecentPackageClicked);
EnvironmentOptions.RecentPackageFiles,
@MainIDEitmOpenRecentPackageClicked);
end;
procedure TPkgManager.AddToMenuRecentPackages(const Filename: string);