Recent

Author Topic: Source Editor - Open Source Folder Option  (Read 5136 times)

440bx

  • Hero Member
  • *****
  • Posts: 5080
Re: Source Editor - Open Source Folder Option
« Reply #30 on: January 29, 2025, 01:04:38 am »
(so my first round to truncate the path in the middle I was using a stringlist for each character... iterating and removing etc...  :-[ .... but yeah... i had a working mess and made it more proper with a little more research)
That's normal.  The first go round is usually to figure out how to solve the problem.  Once a solution is found then a better one is usually within reach as a result of the understanding gained in the first implementation.

Personally, I believe that a good program is one that is written at least _twice_.  The first to learn how to do it right, the second time to, hopefully, do it right and, if it only takes two times, the problem is probably not very complex.

You're on the right track.

(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Tony Stone

  • Sr. Member
  • ****
  • Posts: 268
Re: Source Editor - Open Source Folder Option
« Reply #31 on: January 29, 2025, 04:41:45 am »

But ideally avoid custom pant (for text panels)
grrrr... just spent some time reverting back to basic text and setting the text property of the panel.  Then spent an hour thinking my cliptexttofit function is broke because it is adding the ellipsis at the end no matter what.  And I just now realized that is the default behavior for the panel when text doesn't fit is to add an ellipsis at the end.

Also I cant change font properties of the text property to make it underline when the mouse is over. 

Do you agree because of these limitations we must use the owner drawn style for this panel?


Ok now this is probably stupid to think but I am just wondering... at what point does the current status bars functionality push a developer into using a different control all together?  Like in Visual Studio Code the status bar is pretty much a very short toolbar.  And I don't really like the look.  I guess my point is this:  If you want to keep adding functionality to a statusbar it will require much custom drawing etc.  Is it reasonable to eventually just switch it out to a toolbar?  I mean... is this that bad of a thought?  Feel free to tell me to just go to sleep.


n7800

  • Sr. Member
  • ****
  • Posts: 266
Re: Source Editor - Open Source Folder Option
« Reply #32 on: January 29, 2025, 04:59:52 pm »
Unfortunately, I also didn't find a way to change only "drawing parameters" (background color and font), as it can be done in some components (e.g. TStringGrid.OnPrepareCanvas event).

But I'm not at all sure how the toolbar will look in different OS and widget sets. It will hardly look like a real status bar.

So, I think custom drawing is better and easier than replacing the component. But this is just my opinion.

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4565
  • I like bugs.
Re: Source Editor - Open Source Folder Option
« Reply #33 on: February 01, 2025, 05:52:25 pm »
Some news.
I implemented a new function SelectInFolder in LclIntf, next to OpenDocument. It is now used in the source editor's StatusBar.
A cool feature!
The function name is from suggestions by n7800. The code is basically by Tony Stone, although heavily modified.

I did not touch the appearance or GUI of the source editor's StatusBar. That is another issue and may not be trivial because it requires custom drawing. Patches are welcome.

The new function is properly implemented only for Linux now. On other widgetsets it falls back to opening the file's path in a file manager without selecting anything.
Others please implement it properly. Patches are welcome again.

Tony Stone's patch has
Code: Pascal  [Select][+][-]
  1. {$IFDEF Windows}
  2. FileManagerCmd := 'explorer /select,"' + SourceFilePath + '"'; //unable to test
  3. {$ENDIF}
  4.  
  5. {$IFDEF Darwin}
  6. FileManagerCmd := '/usr/bin/open -R "' + SourceFilePath + '"'; //unable to test
  7. {$ENDIF}
I tested the Windows part but it doesn't work. I cannot test MacOS either.
« Last Edit: February 01, 2025, 09:36:38 pm by JuhaManninen »
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

silvercoder70

  • Full Member
  • ***
  • Posts: 158
    • Tim Coates
Re: Source Editor - Open Source Folder Option
« Reply #34 on: February 01, 2025, 10:09:37 pm »
Explore the beauty of modern Pascal programming with Delphi & Free Pascal - https://www.youtube.com/@silvercoder70

n7800

  • Sr. Member
  • ****
  • Posts: 266
Re: Source Editor - Open Source Folder Option
« Reply #35 on: February 02, 2025, 12:17:23 am »

Tony Stone's patch has
Code: Pascal  [Select][+][-]
  1. {$IFDEF Windows}
  2. FileManagerCmd := 'explorer /select,"' + SourceFilePath + '"'; //unable to test
  3. {$ENDIF}
I tested the Windows part but it doesn't work.

I already explained above why it doesn't work. The command line is correct, but the code below selects /bin/sh for all platforms:

Code: Pascal  [Select][+][-]
  1.     AProcess.Executable := '/bin/sh';
  2.     AProcess.Parameters.Add('-c');
  3.     AProcess.Parameters.Add(FileManagerCmd);
  4.     AProcess.Options := AProcess.Options + [poNoConsole, poDetached];
  5.     AProcess.Execute;
  6.  

Of course, there is no bash on Windows.

n7800

  • Sr. Member
  • ****
  • Posts: 266
Re: Source Editor - Open Source Folder Option
« Reply #36 on: February 02, 2025, 12:44:58 am »
Juha, I noticed in commit 28ba5306 the use of the RunCmdFromPath procedure. Isn't it easier to just call ExecuteProcess? This function even automatically puts quotes around each parameter.

It definitely works on Windows (I recommend leaving comment in the code):
Code: Pascal  [Select][+][-]
  1. // the comma at the end "select" is required
  2. ExecuteProcess('explorer.exe', ['/select,', SourceFilePath]);
  3.  

For other platforms, see silvercoder70's proposal. Also, please see the comments under your commit.

Tony Stone

  • Sr. Member
  • ****
  • Posts: 268
Re: Source Editor - Open Source Folder Option
« Reply #37 on: February 02, 2025, 03:23:07 am »
Oops, sorry guys, I meant to post this.  This was my last update and I got side tracked.  I tested on what you see in the comments.  I think I properly implemented the IF Defined as n7800 suggested.  Got really side tracked.


Code: Pascal  [Select][+][-]
  1. procedure SelectInFolder(const FileDir: string; const SourceFilePath: string);
  2. var
  3.   AProcess: TProcess;
  4.   FileManagerCmd: string;
  5. begin
  6.   {$IF Defined(Linux)}
  7.   if Pos('GNOME', GetEnvironmentVariable('XDG_CURRENT_DESKTOP')) > 0 then
  8.     FileManagerCmd := 'nautilus --select "' + SourceFilePath + '"'
  9.   else if Pos('KDE', GetEnvironmentVariable('XDG_CURRENT_DESKTOP')) > 0 then
  10.     FileManagerCmd := 'dolphin --select "' + SourceFilePath + '"'
  11.   else if FileExists('/usr/bin/nautilus') then
  12.     FileManagerCmd := 'nautilus --select "' + SourceFilePath + '"' //tested - WORKS!
  13.   else if FileExists('/usr/bin/dolphin') then
  14.     FileManagerCmd := 'dolphin --select "' + SourceFilePath + '"' //tested - WORKS!
  15.   else if FileExists('/usr/bin/thunar') then
  16.     FileManagerCmd := 'thunar "' + FileDir + '"' // opens folder but can't select
  17.   else if FileExists('/usr/bin/nemo') then
  18.     FileManagerCmd := 'nemo --no-desktop --browser "' + SourceFilePath + '"' //tested - WORKS!
  19.   else if FileExists('/usr/bin/caja') then
  20.     FileManagerCmd := 'caja --no-desktop --browser "' + SourceFilePath + '"' //might work?
  21.   else if FileExists('/usr/bin/krusader') then
  22.     FileManagerCmd := 'krusader --left "' + FileDir + '"' //opens in left pane
  23.   else
  24.     FileManagerCmd := 'xdg-open "' + FileDir + '"'; //fallback
  25.  
  26.  
  27.   FileManagerCmd := '/bin/sh -c "' + FileManagerCmd + '"';
  28.  
  29.  
  30.   {$ELSEIF Defined(Windows)}
  31.   FileManagerCmd := 'explorer.exe /select,"' + SourceFilePath + '"'; //needs testing
  32.   {$ELSEIF Defined(Darwin)}
  33.   FileManagerCmd := '/usr/bin/open -R "' + SourceFilePath + '"';     //needs test
  34.   {$ELSE}
  35.   // Fallback for unsupported platforms
  36.   OpenDocument(FileDir);
  37.   Exit;
  38.   {$IFEND}
  39.  
  40.  
  41.   AProcess := TProcess.Create(nil);
  42.   try
  43.     {$IF Defined(Linux)}
  44.     AProcess.Executable := '/bin/sh';
  45.     AProcess.Parameters.Add('-c');
  46.     AProcess.Parameters.Add(FileManagerCmd);
  47.     {$ELSE}
  48.     AProcess.CommandLine := FileManagerCmd;
  49.     {$IFEND}
  50.  
  51.  
  52.     AProcess.Options := AProcess.Options + [poNoConsole, poDetached];
  53.     AProcess.Execute;
  54.   finally
  55.     AProcess.Free;
  56.   end;end;      

Tony Stone

  • Sr. Member
  • ****
  • Posts: 268
Re: Source Editor - Open Source Folder Option
« Reply #38 on: February 02, 2025, 03:30:09 am »
Ok and one more block of semi functioning code I want to share because I mentioned I would.  It had an issue and like I said I was side tracked and never finished it.  It lets you truncate the left middle or right.  I had an odd issue with it and dont remeber what though. 

Code: Pascal  [Select][+][-]
  1. type
  2.   TTrimPosition = (tpMiddle, tpLeft, tpRight);
  3.  
  4. function ClipTextToFit(const ACanvas: TCanvas; const S: string;
  5.   MaxWidth: integer; TrimPosition: TTrimPosition = tpMiddle;
  6.   const Indicator: string = '...'): string;
  7. var
  8.   TextWidth, IndicatorWidth: integer;
  9.   LeftLength, RightLength: integer;
  10. begin
  11.  
  12.   if S = '' then
  13.   begin
  14.     Result := '';
  15.     Exit;
  16.   end;
  17.  
  18.   if MaxWidth <= 0 then
  19.     MaxWidth := ACanvas.ClipRect.Right - ACanvas.ClipRect.Left;
  20.  
  21.   TextWidth := ACanvas.TextWidth(S);
  22.   if TextWidth <= MaxWidth then
  23.   begin
  24.     Result := S;
  25.     Exit;
  26.   end;
  27.  
  28.   IndicatorWidth := ACanvas.TextWidth(Indicator);
  29.  
  30.  
  31.   if IndicatorWidth > MaxWidth then
  32.   begin
  33.     Result := Indicator;
  34.     Exit;
  35.   end;
  36.  
  37.   MaxWidth := MaxWidth - IndicatorWidth;
  38.  
  39.   case TrimPosition of
  40.     tpMiddle:
  41.     begin
  42.       LeftLength := Length(S) div 2;
  43.       RightLength := Length(S) div 2;
  44.       while (LeftLength > 0) and (ACanvas.TextWidth(Copy(S, 1, LeftLength)) +
  45.           ACanvas.TextWidth(Copy(S, Length(S) - RightLength + 1, RightLength)) >
  46.           MaxWidth) do
  47.       begin
  48.         if LeftLength > RightLength then
  49.           Dec(LeftLength)
  50.         else
  51.           Dec(RightLength);
  52.       end;
  53.       Result := Copy(S, 1, LeftLength) + Indicator +
  54.         Copy(S, Length(S) - RightLength + 1, RightLength);
  55.     end;
  56.  
  57.     tpLeft:
  58.     begin
  59.       RightLength := Length(S);
  60.       while (RightLength > 0) and
  61.         (ACanvas.TextWidth(Copy(S, Length(S) - RightLength + 1, RightLength)) >
  62.           MaxWidth) do
  63.       begin
  64.         Dec(RightLength);
  65.       end;
  66.       Result := Indicator + Copy(S, Length(S) - RightLength + 1, RightLength);
  67.     end;
  68.  
  69.     tpRight:
  70.     begin
  71.       LeftLength := Length(S);
  72.       while (LeftLength > 0) and (ACanvas.TextWidth(Copy(S, 1, LeftLength)) >
  73.           MaxWidth) do
  74.       begin
  75.         Dec(LeftLength);
  76.       end;
  77.  
  78.       Result := Copy(S, 1, LeftLength) + Indicator;
  79.     end;
  80.   end;
  81. end;
« Last Edit: February 02, 2025, 03:37:04 am by Tony Stone »

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4565
  • I like bugs.
Re: Source Editor - Open Source Folder Option
« Reply #39 on: February 02, 2025, 09:46:43 am »
I have implemented the Windows version of the new function. Just a quote char was wrong, now it works.
Please test.

Juha, I noticed in commit 28ba5306 the use of the RunCmdFromPath procedure. Isn't it easier to just call ExecuteProcess? This function even automatically puts quotes around each parameter.
Maybe yes. It would be worth testing. RunCmdFromPath has this important line:
Code: Pascal  [Select][+][-]
  1. NewProgramFilename:=FindFilenameOfCmd(ProgramFilename);
For example "dolphin" becomes "/usr/bin/dolphin". A simple TProcess does not work without a full path.

I think I properly implemented the IF Defined as n7800 suggested.
I believe you did but neither "IF Defined" nor "IFDEF" is needed in my code for LCL. The IDFEFs happen earlier in unit LCLIntf.
Please see the latest Lazarus trunk code. Now the function is implemented also for MacOS thanks to rich2014.

Patches are welcome if you find ways to improve the code.
« Last Edit: February 02, 2025, 09:50:29 am by JuhaManninen »
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

n7800

  • Sr. Member
  • ****
  • Posts: 266
Re: Source Editor - Open Source Folder Option
« Reply #40 on: February 07, 2025, 05:10:04 am »
Tony Stone, I saw your comment, but let's continue here. Discussions under commits are not welcome, as they are hard to find later. Our conversation of six users has dragged on anyway ))

Quote
I think I did it like this because I had concerns with directly using the string value of a GUI component... Maybe I was only concerned because I was truncating it.

When the shortening function is ready, of course, the path will need to be read from another place.

Quote
Code: Pascal  [Select][+][-]
  1.   // should deal with an unsaved file but what about a "virtual file?"
  2.   if not FileExists(SourceFilePath) then
  3.   begin
  4.     if CurEditor.Modified then
  5.       ShowMessage('File not saved, cannot open folder.')
  6.     else
  7.       ShowMessage('Cannot find file on disk: ' + SourceFilePath);
  8.     Exit;
  9.   end;
  10.  

Yes, you need to check if the file is new (virtual, not saved yet). But you also need to check if it is a file at all (for example, an IDE script can be opened). Only then you need to check the existence of the file:

Code: Pascal  [Select][+][-]
  1. if ActiveUnitInfo.InternalFile then exit;
  2. if ActiveUnitInfo.IsVirtual then exit;
  3. if not FileExistsUTF8(SourceFilePath) then exit;
  4.  

This is just a code snippet. I will soon finish the patch for the context menu, and the same code can be used for the status bar. Also, there is a unit dependency issue, so the code itself should be in a different place.

By the way, I don't think that "CurEditor.Modified" needs to be checked. For example, sometimes I specifically want to open the original file (before saving) to look at the original.

n7800

  • Sr. Member
  • ****
  • Posts: 266
Re: Source Editor - Open Source Folder Option
« Reply #41 on: February 07, 2025, 05:13:12 am »
By the way, I was surprised, but GetActiveEditor correctly returns the editor when double-clicking on the status bar of another editor (which is not in focus). The thing is that even after the click, the focus remains in the old editor (the docked version).

 

TinyPortal © 2005-2018