-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for RichTextBlock (#220)
* Basic bindings for RichTextBlock * Initial DSL for inline elements * Use differ on inline elements * Switch to IView + generalize VDOM to IAvaloniaObject * Remove inline types * Remove control casts * Add styling via TextElement * Add rest of inline elements This adds Bold, Italic, LineBreak, Span and Underline. * Do not re-initialize brush on every switch
- Loading branch information
1 parent
3a32ee2
commit eaebd3f
Showing
22 changed files
with
361 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
namespace Avalonia.FuncUI.DSL | ||
|
||
open Avalonia.FuncUI.Types | ||
|
||
[<AutoOpen>] | ||
module Bold = | ||
open Avalonia.FuncUI.Builder | ||
open Avalonia.Controls.Documents | ||
|
||
let create (attrs: IAttr<Bold> list): IView<Bold> = | ||
ViewBuilder.Create(attrs) | ||
|
||
let simple (text: string): IView<Bold> = | ||
ViewBuilder.Create([ | ||
Bold.inlines [ | ||
Run.create [ | ||
Run.text text | ||
] :> IView | ||
] | ||
]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
namespace Avalonia.FuncUI.DSL | ||
|
||
[<AutoOpen>] | ||
module Italic = | ||
open Avalonia.FuncUI.Builder | ||
open Avalonia.FuncUI.Types | ||
open Avalonia.Controls.Documents | ||
|
||
let create (attrs: IAttr<Italic> list): IView<Italic> = | ||
ViewBuilder.Create(attrs) | ||
|
||
let simple (text: string): IView<Italic> = | ||
ViewBuilder.Create([ | ||
Italic.inlines [ | ||
Run.create [ | ||
Run.text text | ||
] :> IView | ||
] | ||
]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace Avalonia.FuncUI.DSL | ||
|
||
[<AutoOpen>] | ||
module LineBreak = | ||
open Avalonia.FuncUI.Builder | ||
open Avalonia.FuncUI.Types | ||
open Avalonia.Controls.Documents | ||
|
||
let create (attrs: IAttr<LineBreak> list): IView<LineBreak> = | ||
ViewBuilder.Create(attrs) | ||
|
||
/// Creates a simple line-break with no attributes. | ||
let simple : IView<LineBreak> = | ||
create([]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace Avalonia.FuncUI.DSL | ||
|
||
[<AutoOpen>] | ||
module Run = | ||
open Avalonia.FuncUI.Builder | ||
open Avalonia.FuncUI.Types | ||
open Avalonia.Controls.Documents | ||
|
||
let create (attrs: IAttr<Run> list) : IView<Run> = ViewBuilder.Create(attrs) | ||
|
||
type Run with | ||
static member text<'t when 't :> Run>(value: string) : IAttr<'t> = | ||
AttrBuilder<'t>.CreateProperty<string>(Run.TextProperty, value, ValueNone) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
namespace Avalonia.FuncUI.DSL | ||
|
||
[<AutoOpen>] | ||
module Span = | ||
open Avalonia.FuncUI.Builder | ||
open Avalonia.FuncUI.Types | ||
open Avalonia.Controls.Documents | ||
|
||
let create (attrs: IAttr<Span> list): IView<Span> = | ||
ViewBuilder.Create(attrs) | ||
|
||
type Span with | ||
static member inlines<'t when 't :> Span>(value: InlineCollection) : IAttr<'t> = | ||
AttrBuilder<'t>.CreateProperty<InlineCollection>(Span.InlinesProperty, value, ValueNone) | ||
|
||
static member inlines<'t when 't :> Span>(values: IView list (* TODO: Change to IView<Inline> *)) : IAttr<'t> = | ||
let getter : ('t -> obj) = (fun control -> control.Inlines :> obj) | ||
AttrBuilder<'t>.CreateContentMultiple("Inlines", ValueSome getter, ValueNone, values) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
namespace Avalonia.FuncUI.DSL | ||
|
||
[<AutoOpen>] | ||
module Underline = | ||
open Avalonia.FuncUI.Builder | ||
open Avalonia.FuncUI.Types | ||
open Avalonia.Controls.Documents | ||
|
||
let create (attrs: IAttr<Underline> list): IView<Underline> = | ||
ViewBuilder.Create(attrs) | ||
|
||
let simple (text: string): IView<Underline> = | ||
ViewBuilder.Create([ | ||
Underline.inlines [ | ||
Run.create [ | ||
Run.text text | ||
] :> IView | ||
] | ||
]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
namespace Avalonia.FuncUI.DSL | ||
|
||
open Avalonia.Controls.Documents | ||
|
||
[<AutoOpen>] | ||
module TextElement = | ||
open Avalonia.Media.Immutable | ||
open Avalonia.FuncUI.Types | ||
open Avalonia.FuncUI.Builder | ||
open Avalonia.Media | ||
|
||
let create (attrs: IAttr<TextElement> list): IView<TextElement> = | ||
ViewBuilder.Create<TextElement>(attrs) | ||
|
||
type TextElement with | ||
static member background<'t when 't :> TextElement>(value: IBrush) : IAttr<'t> = | ||
AttrBuilder<'t>.CreateProperty<IBrush>(TextElement.BackgroundProperty, value, ValueNone) | ||
|
||
static member background<'t when 't :> TextElement>(color: string) : IAttr<'t> = | ||
Color.Parse(color) |> ImmutableSolidColorBrush |> TextElement.background | ||
|
||
static member fontFamily<'t when 't :> TextElement>(value: FontFamily) : IAttr<'t> = | ||
AttrBuilder<'t>.CreateProperty<FontFamily>(TextElement.FontFamilyProperty, value, ValueNone) | ||
|
||
static member fontSize<'t when 't :> TextElement>(value: double) : IAttr<'t> = | ||
AttrBuilder<'t>.CreateProperty<double>(TextElement.FontSizeProperty, value, ValueNone) | ||
|
||
static member fontStyle<'t when 't :> TextElement>(value: FontStyle) : IAttr<'t> = | ||
AttrBuilder<'t>.CreateProperty<FontStyle>(TextElement.FontStyleProperty, value, ValueNone) | ||
|
||
static member fontStretch<'t when 't :> TextElement>(value: FontStretch) : IAttr<'t> = | ||
AttrBuilder<'t>.CreateProperty<FontStretch>(TextElement.FontStretchProperty, value, ValueNone) | ||
|
||
static member fontWeight<'t when 't :> TextElement>(value: FontWeight) : IAttr<'t> = | ||
AttrBuilder<'t>.CreateProperty<FontWeight>(TextElement.FontWeightProperty, value, ValueNone) | ||
|
||
static member foreground<'t when 't :> TextElement>(value: IBrush) : IAttr<'t> = | ||
AttrBuilder<'t>.CreateProperty<IBrush>(TextElement.ForegroundProperty, value, ValueNone) | ||
|
||
static member foreground<'t when 't :> TextElement>(color: string) : IAttr<'t> = | ||
Color.Parse(color) |> ImmutableSolidColorBrush |> TextElement.foreground |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
namespace Avalonia.FuncUI.DSL | ||
|
||
|
||
[<AutoOpen>] | ||
module RichTextBlock = | ||
open Avalonia.Controls | ||
open Avalonia.Controls.Documents | ||
open Avalonia.FuncUI.Builder | ||
open Avalonia.FuncUI.Types | ||
|
||
let create (attrs: IAttr<RichTextBlock> list): IView<RichTextBlock> = | ||
ViewBuilder.Create<RichTextBlock>(attrs) | ||
|
||
type RichTextBlock with | ||
static member inlines<'t when 't :> RichTextBlock>(value: InlineCollection) : IAttr<'t> = | ||
AttrBuilder<'t>.CreateProperty<InlineCollection>(RichTextBlock.InlinesProperty, value, ValueNone) | ||
|
||
static member inlines<'t when 't :> RichTextBlock>(values: IView list (* TODO: Change to IView<Inline> *)) : IAttr<'t> = | ||
let getter : ('t -> obj) = (fun control -> control.Inlines :> obj) | ||
AttrBuilder<'t>.CreateContentMultiple("Inlines", ValueSome getter, ValueNone, values) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.