Przeglądaj źródła

ADD: Show progress form when connect network drive

Alexander Koblov 2 lat temu
rodzic
commit
cd52866a71
1 zmienionych plików z 126 dodań i 3 usunięć
  1. 126 3
      src/platform/win/unetworkthread.pas

+ 126 - 3
src/platform/win/unetworkthread.pas

@@ -5,7 +5,8 @@ unit uNetworkThread;
 interface
 interface
 
 
 uses
 uses
-  Classes, SysUtils, SyncObjs, JwaWinNetWk, Windows, Forms, Graphics, uDrive;
+  Classes, SysUtils, SyncObjs, JwaWinNetWk, Windows, Forms, Graphics, Dialogs,
+  StdCtrls, ComCtrls, Buttons, uDrive, uOSForms;
 
 
 type
 type
 
 
@@ -18,6 +19,19 @@ type
     destructor Destroy; override;
     destructor Destroy; override;
   end;
   end;
 
 
+  { TNetworkForm }
+
+  TNetworkForm = class(TModalDialog)
+    lblPrompt: TLabel;
+    btnAbort: TBitBtn;
+    pbConnect: TProgressBar;
+  private
+    FThread: TThread;
+  public
+    constructor Create(TheOwner: TComponent; AThread: TThread; APath: PWideChar); reintroduce;
+    procedure ExecuteModal; override;
+  end;
+
   { TNetworkThread }
   { TNetworkThread }
 
 
   TNetworkThread = class(TThread)
   TNetworkThread = class(TThread)
@@ -31,7 +45,8 @@ type
     constructor Create(lpLocalName, lpRemoteName: LPWSTR; dwType: DWORD); reintroduce;
     constructor Create(lpLocalName, lpRemoteName: LPWSTR; dwType: DWORD); reintroduce;
     destructor Destroy; override;
     destructor Destroy; override;
   public
   public
-    class function Connect(lpLocalName, lpRemoteName: LPWSTR; dwType: DWORD; CheckOperationState: TThreadMethod = nil): Integer;
+    class function Connect(lpLocalName, lpRemoteName: LPWSTR; dwType: DWORD): Integer; overload;
+    class function Connect(lpLocalName, lpRemoteName: LPWSTR; dwType: DWORD; CheckOperationState: TThreadMethod): Integer; overload;
   end;
   end;
 
 
   { TNetworkDriveLoader }
   { TNetworkDriveLoader }
@@ -51,7 +66,7 @@ type
 implementation
 implementation
 
 
 uses
 uses
-   uMyWindows, uPixMapManager;
+  Math, InterfaceBase, Controls, fMain, uMyWindows, uPixMapManager, uLng;
 
 
 { TDriveIcon }
 { TDriveIcon }
 
 
@@ -86,6 +101,78 @@ begin
   FreeOnTerminate:= True;
   FreeOnTerminate:= True;
 end;
 end;
 
 
+{ TNetworkForm }
+
+constructor TNetworkForm.Create(TheOwner: TComponent; AThread: TThread;
+  APath: PWideChar);
+begin
+  FThread:= AThread;
+  inherited CreateNew(TheOwner);
+
+  AutoSize:= True;
+  BorderStyle:= bsDialog;
+  Caption:= Application.Title;
+  Position:= poOwnerFormCenter;
+  ChildSizing.TopBottomSpacing:= 6;
+  ChildSizing.LeftRightSpacing:= 6;
+
+  lblPrompt := TLabel.Create(Self);
+  with lblPrompt do
+  begin
+    Parent:= Self;
+    AutoSize:= True;
+    Caption:= rsOperWaitingForConnection + ' ' + UTF8Encode(UnicodeString(APath));
+  end;
+  pbConnect:= TProgressBar.Create(Self);
+  with pbConnect do
+  begin
+    Parent:= Self;
+    Style:= pbstMarquee;
+    AnchorToNeighbour(akTop, 6, lblPrompt);
+    Constraints.MinWidth:= Math.Max(280, Screen.Width div 4);
+  end;
+  btnAbort:= TBitBtn.Create(Self);
+  with btnAbort do
+  begin
+    Parent:= Self;
+    Kind:= bkAbort;
+    Default:= True;
+    Cancel:= True;
+    AutoSize:= True;
+    AnchorHorizontalCenterTo(Self);
+    AnchorToNeighbour(akTop, 12, pbConnect);
+  end;
+end;
+
+procedure TNetworkForm.ExecuteModal;
+begin
+  repeat
+    WidgetSet.AppProcessMessages;
+
+    if Application.Terminated then
+    begin
+      ModalResult:= mrCancel;
+    end;
+
+    if ModalResult <> 0 then
+    begin
+      CloseModal;
+      if ModalResult <> 0 then Break;
+    end;
+
+    with TNetworkThread(FThread) do
+    begin
+      if (FWaitConnect.WaitFor(1) <> wrTimeout) then
+      begin
+        ModalResult:= mrOK;
+        Break;
+      end;
+    end;
+
+    Application.Idle(True);
+  until False;
+end;
+
 { TNetworkThread }
 { TNetworkThread }
 
 
 procedure TNetworkThread.Execute;
 procedure TNetworkThread.Execute;
@@ -122,6 +209,42 @@ begin
   inherited Destroy;
   inherited Destroy;
 end;
 end;
 
 
+class function TNetworkThread.Connect(lpLocalName, lpRemoteName: LPWSTR;
+  dwType: DWORD): Integer;
+var
+  AStartTime: QWord;
+  AThread: TNetworkThread;
+begin
+  AThread:= TNetworkThread.Create(lpLocalName, lpRemoteName, dwType);
+  with AThread do
+  begin
+    Start;
+    AStartTime:= GetTickCount64;
+    try
+      while True do
+      begin
+        if (GetTickCount64 - AStartTime > 3000) then
+        begin
+          with TNetworkForm.Create(frmMain, AThread, lpRemoteName) do
+          try
+            if (ShowModal = mrOK) then
+              Exit(ReturnValue)
+            else begin
+              Exit(ERROR_CANCELLED);
+            end;
+          finally
+            Free;
+          end;
+        end;
+        if (GetAsyncKeyStateEx(VK_ESCAPE)) then Exit(ERROR_CANCELLED);
+        if (FWaitConnect.WaitFor(1) <> wrTimeout) then Exit(ReturnValue);
+      end;
+    finally
+      FWaitFinish.SetEvent;
+    end;
+  end;
+end;
+
 class function TNetworkThread.Connect(lpLocalName, lpRemoteName: LPWSTR;
 class function TNetworkThread.Connect(lpLocalName, lpRemoteName: LPWSTR;
   dwType: DWORD; CheckOperationState: TThreadMethod): Integer;
   dwType: DWORD; CheckOperationState: TThreadMethod): Integer;
 begin
 begin