11import { Theme , Store , Logic , Icons , Util } from "../../def.slint" ;
2- import { ChatEntry , MdElementType , MdImage , MdMath , MdElement , MdUrl , MdHeading , MdUrl , MdListItem , MdTable } from "../../../store.slint" ;
2+ import { ChatEntry , MdElementType , MdImage , MdMath , MdElement , MdUrl , MdHeading , MdUrl , MdListItem , MdTable , MdCodeBlock } from "../../../store.slint" ;
33import { IconBtn , Label , Link , ToolTipSetting , ComponentPosition } from "../../../base/widgets.slint" ;
44import { SearchLink } from "../../../store.slint" ;
5+ import { ScrollView } from "std-widgets.slint" ;
56
67export component MdText inherits HorizontalLayout {
78 in-out property text <=> txt.text;
@@ -66,7 +67,7 @@ component MdListItem inherits HorizontalLayout {
6667}
6768
6869export component MdCodeBlock inherits HorizontalLayout {
69- in-out property <string > code ;
70+ in-out property <MdCodeBlock > code-block ;
7071
7172 padding-bottom : Theme.padding * 4 ;
7273
@@ -79,39 +80,83 @@ export component MdCodeBlock inherits HorizontalLayout {
7980 background : Theme.code-header-color;
8081
8182 HorizontalLayout {
82- alignment : LayoutAlignment.end ;
83+ alignment : LayoutAlignment.space-between ;
8384 padding : Theme.padding * 2 ;
8485 padding-right : Theme.padding * 3 ;
86+ padding-left : Theme.padding * 4 ;
8587
86- IconBtn {
87- icon : Icons.copy;
88- tip : Logic.tr("copy" );
89- is-show-tip : true ;
90- gain-focus-when-clicked : false ;
88+ Label {
89+ text : code-block.lang;
90+ }
91+
92+ HorizontalLayout {
93+ spacing : Theme.spacing * 4 ;
94+
95+ IconBtn {
96+ icon : Icons.copy;
97+ tip : Logic.tr("copy" );
98+ is-show-tip : true ;
99+ gain-focus-when-clicked : false ;
91100
92- clicked => {
93- Logic.copy-to-clipboard (code);
101+ clicked => {
102+ Logic.copy-to-clipboard (code-block.code);
103+ }
94104 }
95105 }
96106 }
97107 }
98108
109+ // `ScrollView` could catch scroll event. However, short content should not catch scroll event
99110 Rectangle {
100- border-bottom-left-radius : Theme.border-radius;
101- border-bottom-right-radius : Theme.border-radius;
102- background : Theme.thirdly-background;
111+ private property <float > rate : 0.5 ;
112+ private property <bool > use-scrollview : txt-rect.preferred-height > (Theme.default-height * rate);
103113
104- HorizontalLayout {
105- padding : Theme.padding * 4 ;
106- padding-top : Theme.padding * 2 ;
107-
108- txt := TextInput {
109- wrap : word-wrap;
110- color : Theme.primary-text-color;
111- font-size : Theme.title4 -font-size;
112- single-line : false ;
113- read-only : true ;
114- text : code;
114+ height : use-scrollview ? Theme.default-height * rate : txt-rect.preferred-height;
115+
116+ Rectangle {
117+ visible : !use-scrollview;
118+ border-bottom-left-radius : Theme.border-radius;
119+ border-bottom-right-radius : Theme.border-radius;
120+ background : Theme.thirdly-background;
121+
122+ HorizontalLayout {
123+ padding : Theme.padding * 4 ;
124+ padding-top : Theme.padding * 2 ;
125+
126+ TextInput {
127+ wrap : word-wrap;
128+ color : Theme.primary-text-color;
129+ font-size : Theme.title4 -font-size;
130+ single-line : false ;
131+ read-only : true ;
132+ text : code-block.code;
133+ }
134+ }
135+ }
136+
137+ ScrollView {
138+ visible : use-scrollview;
139+ viewport-height : txt-rect.preferred-height;
140+ height : Math.min(txt-rect.preferred-height, Theme.default-height * rate);
141+
142+ txt-rect := Rectangle {
143+ border-bottom-left-radius : Theme.border-radius;
144+ border-bottom-right-radius : Theme.border-radius;
145+ background : Theme.thirdly-background;
146+
147+ HorizontalLayout {
148+ padding : Theme.padding * 4 ;
149+ padding-top : Theme.padding * 2 ;
150+
151+ txt := TextInput {
152+ wrap : word-wrap;
153+ color : Theme.primary-text-color;
154+ font-size : Theme.title4 -font-size;
155+ single-line : false ;
156+ read-only : true ;
157+ text : code-block.code;
158+ }
159+ }
115160 }
116161 }
117162 }
@@ -284,7 +329,7 @@ export component MdDetail inherits Rectangle {
284329 }
285330
286331 if entry.ty == MdElementType.CodeBlock: MdCodeBlock {
287- code : entry.code_block ;
332+ code-block : entry.code-block ;
288333 }
289334
290335 if entry.ty == MdElementType.Math: MdMath {
0 commit comments