瀏覽代碼

designer: eat parser exceptions, fixed style inspector cleanup

mattias 21 小時之前
父節點
當前提交
6ce3510753

+ 2 - 1
demo/Inspector/InspectorDemo.lpi

@@ -42,7 +42,8 @@
       <Unit>
         <Filename Value="InspDemoForm1.pas"/>
         <IsPartOfProject Value="True"/>
-        <ComponentName Value="FresnelForm1"/>
+        <ComponentName Value="DemoForm1"/>
+        <HasResources Value="True"/>
         <ResourceBaseClass Value="Other"/>
         <ResourceBaseClassname Value="TFresnelForm"/>
       </Unit>

+ 1 - 0
design/fresnel.dsgninspector.pas

@@ -63,6 +63,7 @@ begin
   FrlControl.Name:='FrlControl';
   FrlControl.Align:=alClient;
   FrlControl.Parent:=Self;
+  FrlControl.Viewport.Stylesheet.Text:=':root { background-color: #222; }';
 
   CSSStyleInspector:=TCSSStyleInspector.Create(Self);
   CSSStyleInspector.Name:='CSSStyleInspector';

+ 2 - 14
design/fresnel.register.pas

@@ -711,18 +711,12 @@ end;
 procedure TFresnelStylePropertyEditor.Edit;
 var
   TheDialog : TStylePropEditDialog;
-  AString: String;
 begin
-  AString := GetStrValue; // read first to get nicer error messages
-
   TheDialog := TStylePropEditDialog.Create(nil);
   try
     TheDialog.Caption := 'CSS Inline Style Editor';
     TheDialog.Editor := Self;
-    TheDialog.CSSSynEdit.Text := AString;
-    TheDialog.CSSSynEditChange(nil);
-    if TheDialog.ShowModal = mrOK then
-      TheDialog.Apply;
+    TheDialog.ShowModal;
   finally
     TheDialog.Free;
   end;
@@ -733,18 +727,12 @@ end;
 procedure TFresnelStyleSheetPropertyEditor.Edit;
 var
   TheDialog : TStylePropEditDialog;
-  aList: TStrings;
 begin
-  aList := TStrings(GetObjectValue);
-
   TheDialog := TStylePropEditDialog.Create(nil);
   try
     TheDialog.Caption := 'CSS Stylesheet Editor';
     TheDialog.Editor := Self;
-    TheDialog.CSSSynEdit.Text := aList.Text;
-    TheDialog.CSSSynEditChange(nil);
-    if TheDialog.ShowModal = mrOK then
-      TheDialog.Apply;
+    TheDialog.ShowModal;
   finally
     TheDialog.Free;
   end;

+ 18 - 17
design/fresnel.stylepropedit.lfm

@@ -20,22 +20,22 @@ object StylePropEditDialog: TStylePropEditDialog
     AnchorSideRight.Side = asrBottom
     AnchorSideBottom.Control = BtnPanel
     Left = 6
-    Height = 384
+    Height = 387
     Top = 6
     Width = 555
     Align = alTop
     Anchors = [akTop, akLeft, akRight, akBottom]
     BorderSpacing.Around = 6
-    ClientHeight = 382
-    ClientWidth = 553
+    ClientHeight = 360
+    ClientWidth = 545
     TabOrder = 0
     object StatusLabel: TLabel
       AnchorSideBottom.Control = TextGroupBox
       AnchorSideBottom.Side = asrBottom
       Left = 9
       Height = 16
-      Top = 360
-      Width = 70
+      Top = 338
+      Width = 72
       Anchors = [akLeft, akBottom]
       BorderSpacing.Around = 6
       Caption = 'StatusLabel'
@@ -45,9 +45,9 @@ object StylePropEditDialog: TStylePropEditDialog
       AnchorSideRight.Control = SortButton
       AnchorSideBottom.Control = StatusLabel
       Left = 6
-      Height = 354
+      Height = 332
       Top = 0
-      Width = 460
+      Width = 452
       BorderSpacing.Around = 6
       Anchors = [akTop, akLeft, akRight, akBottom]
       Font.Height = 13
@@ -58,7 +58,7 @@ object StylePropEditDialog: TStylePropEditDialog
       ParentColor = False
       ParentFont = False
       TabOrder = 0
-      Gutter.Width = 57
+      Gutter.Width = 67
       Gutter.MouseActions = <>
       RightGutter.Width = 0
       RightGutter.MouseActions = <>
@@ -520,7 +520,7 @@ object StylePropEditDialog: TStylePropEditDialog
           MarkupInfoCurrentLine.Background = clNone
         end
         object SynGutterLineNumber1: TSynGutterLineNumber
-          Width = 17
+          Width = 27
           MouseActions = <>
           MarkupInfo.Background = clBtnFace
           MarkupInfoCurrentLine.Foreground = clNone
@@ -565,8 +565,8 @@ object StylePropEditDialog: TStylePropEditDialog
       AnchorSideTop.Side = asrBottom
       AnchorSideRight.Control = TextGroupBox
       AnchorSideRight.Side = asrBottom
-      Left = 472
-      Height = 30
+      Left = 464
+      Height = 20
       Top = 32
       Width = 75
       Anchors = [akTop, akRight]
@@ -584,9 +584,9 @@ object StylePropEditDialog: TStylePropEditDialog
       AnchorSideTop.Side = asrBottom
       AnchorSideRight.Control = SortButton
       AnchorSideRight.Side = asrBottom
-      Left = 472
-      Height = 30
-      Top = 68
+      Left = 464
+      Height = 20
+      Top = 58
       Width = 75
       Anchors = [akTop, akLeft, akRight]
       AutoSize = True
@@ -600,7 +600,7 @@ object StylePropEditDialog: TStylePropEditDialog
       AnchorSideLeft.Side = asrBottom
       AnchorSideRight.Control = TextGroupBox
       AnchorSideRight.Side = asrBottom
-      Left = 472
+      Left = 464
       Height = 26
       Top = 0
       Width = 75
@@ -611,12 +611,13 @@ object StylePropEditDialog: TStylePropEditDialog
       Caption = 'Apply'
       Down = True
       GroupIndex = 1
+      OnClick = ApplySpeedButtonClick
     end
   end
   object BtnPanel: TButtonPanel
     Left = 6
-    Height = 30
-    Top = 396
+    Height = 27
+    Top = 399
     Width = 555
     OKButton.Name = 'OKButton'
     OKButton.DefaultCaption = True

+ 56 - 2
design/fresnel.stylepropedit.pas

@@ -24,6 +24,7 @@ type
     CSSSynEdit: TSynEdit;
     SynCssSyn1: TSynCssSyn;
     procedure ApplyButtonClick(Sender: TObject);
+    procedure ApplySpeedButtonClick(Sender: TObject);
     procedure ClearButtonClick(Sender: TObject);
     procedure FormClose(Sender: TObject; var {%H-}CloseAction: TCloseAction);
     procedure FormCreate(Sender: TObject);
@@ -32,8 +33,13 @@ type
     procedure SortButtonClick(Sender: TObject);
   private
     FEditor: TPropertyEditor;
+    FOldValue: String;
+    FOldList: TStrings;
+    procedure OnCancelButtonClick(Sender: TObject);
+    procedure OnOkButtonClick(Sender: TObject);
     procedure SetEditor(const AValue: TPropertyEditor);
   public
+    destructor Destroy; override;
     property Editor: TPropertyEditor read FEditor write SetEditor;
     procedure Apply; virtual;
   end;
@@ -57,6 +63,12 @@ begin
   Apply;
 end;
 
+procedure TStylePropEditDialog.ApplySpeedButtonClick(Sender: TObject);
+begin
+  if ApplySpeedButton.Down then
+    Apply;
+end;
+
 procedure TStylePropEditDialog.FormClose(Sender: TObject;
   var CloseAction: TCloseAction);
 begin
@@ -69,9 +81,11 @@ begin
   ApplySpeedButton.Caption := sccsTrEdtApply;
   SortButton.Caption := oisSort;
   SaveButton.Caption := oisSave;
+  BtnPanel.OKButton.OnClick:=@OnOkButtonClick;
+  BtnPanel.CancelButton.OnClick:=@OnCancelButtonClick;
 
   CSSSynEdit.Font.Name := SynDefaultFontName;
-  CSSSynEdit.Font.Height := SynDefaultFontHeight;
+  CSSSynEdit.Font.Height := {$IFDEF LCLCocoa}20{$ELSE}SynDefaultFontHeight{$ENDIF};
   CSSSynEdit.Font.Pitch := SynDefaultFontPitch;
   CSSSynEdit.Font.Quality := SynDefaultFontQuality;
 
@@ -131,21 +145,61 @@ begin
 end;
 
 procedure TStylePropEditDialog.SetEditor(const AValue: TPropertyEditor);
+var
+  aList: TStrings;
 begin
   if FEditor=AValue then Exit;
   FEditor:=AValue;
+  if Editor=nil then exit;
+
   if Editor is TStringPropertyEditor then
+  begin
+    FOldValue:=TStringPropertyEditor(Editor).GetStrValue;
+    CSSSynEdit.Text := FOldValue;
+    CSSSynEditChange(nil);
+  end
   else if Editor is TClassPropertyEditor then
-  else
+  begin
+    aList := TStrings(TClassPropertyEditor(Editor).GetObjectValue);
+    if FOldList=nil then FOldList:=TStringList.Create;
+    FOldList.Assign(aList);
+    CSSSynEdit.Text := aList.Text;
+    CSSSynEditChange(nil);
+  end else
     raise Exception.Create('TStylePropEditDialog.SetEditor '+DbgSName(Editor));
 end;
 
+destructor TStylePropEditDialog.Destroy;
+begin
+  FreeAndNil(FOldList);
+  inherited Destroy;
+end;
+
+procedure TStylePropEditDialog.OnOkButtonClick(Sender: TObject);
+begin
+  Apply;
+end;
+
+procedure TStylePropEditDialog.OnCancelButtonClick(Sender: TObject);
+var
+  aList: TStrings;
+begin
+  if Editor is TStringPropertyEditor then
+    TStringPropertyEditor(Editor).SetStrValue(FOldValue)
+  else if Editor is TClassPropertyEditor then
+  begin
+    aList := TStrings(TClassPropertyEditor(Editor).GetObjectValue);
+    aList.Assign(FOldList);
+  end;
+end;
+
 procedure TStylePropEditDialog.Apply;
 var
   AString: String;
   LineEndPos: SizeInt;
   aList: TStrings;
 begin
+  if csDestroying in ComponentState then exit;
   AString := CSSSynEdit.Text;
   LineEndPos := Length(AString) - Length(LineEnding) + 1;
   // erase the last lineending if any

+ 10 - 4
src/base/fcl-css/fpcssresolver.pas

@@ -892,10 +892,16 @@ begin
     aParser.Resolver:=Self;
     aParser.OnLog:=@Log;
     aParser.CSSNthChildParamsClass:=TCSSResolverNthChildParams;
-    if Inline then
-      Result:=aParser.ParseInline
-    else
-      Result:=aParser.Parse;
+    try
+      if Inline then
+        Result:=aParser.ParseInline
+      else
+        Result:=aParser.Parse;
+    except
+      on E: ECSSException do begin
+        writeln('TCSSResolver.ParseCSSSource ',E.Message);
+      end;
+    end;
   finally
     aParser.Free;
     ms.Free;

+ 15 - 4
src/base/fresnel.cssstyleinspector.pas

@@ -63,6 +63,8 @@ var
   VP: TFresnelViewport;
 begin
   if FTarget=AValue then Exit;
+  if FTarget<>nil then
+    RemoveFreeNotification(FTarget);
   FTarget:=AValue;
   if FTarget<>nil then
   begin
@@ -77,7 +79,8 @@ begin
       if VP.Rendered then
         Inspect;
     end;
-  end;
+  end else
+    Inspect;
 end;
 
 procedure TCSSStyleInspector.OnTargetCSSApplied(Event: TAbstractEvent);
@@ -175,6 +178,9 @@ end;
 procedure TCSSStyleInspector.ShowNothingSelected;
 begin
   UpdateRule(0,ElementStyleCaption,'',nil);
+  // delete old nodes
+  while RulesDiv.NodeCount>1 do
+    RulesDiv.Nodes[RulesDiv.NodeCount-1].Free;
 end;
 
 procedure TCSSStyleInspector.UpdateRule(Row: integer; Selectors, Origin: TCSSString;
@@ -232,11 +238,10 @@ begin
     BracketCloseLabel.Parent:=FooterDiv;
   end;
 
-  // header: "selector {    Origin"
+  SubRow:=1; // +1 for header
 
   if RuleEl<>nil then
   begin
-    SubRow:=1;
     for i:=0 to RuleEl.ChildCount-1 do
     begin
       ChildEl:=RuleEl.Children[i];
@@ -248,6 +253,12 @@ begin
       end;
     end;
   end;
+
+  inc(SubRow); // +1 for footer
+
+  // free old rows
+  while CurDiv.NodeCount > SubRow do
+    CurDiv.Nodes[CurDiv.NodeCount-2].Free;
 end;
 
 procedure TCSSStyleInspector.UpdateDeclaration(RuleDiv: TDiv; Row: integer;
@@ -402,7 +413,7 @@ end;
 class function TCSSStyleInspector.GetCSSTypeStyle: TCSSString;
 begin
   Result:=
-     '.'+RulesDivClass+' { color: #fff; font-size: 15px; background-color: #333 }'+LineEnding
+     '.'+RulesDivClass+' { color: #fff; font-size: 15px; background-color: #222; padding: 6px; }'+LineEnding
     +'.'+RuleSelectorLabelClass+' { margin-right: 1ch; }'+LineEnding
     +'.'+RuleBracketLabelClass+' { font-weight: bold; }'+LineEnding
     +'.'+RuleOriginLabelClass+' { margin-left: 5ch; }'+LineEnding