Package swingtree.style
Class TextSelectionConf
java.lang.Object
swingtree.style.TextSelectionConf
Describes how the selected characters of a styled text look, inside the text style
of a component:
UI.box()
.withStyle( it -> it
.text("body", t -> t
.content("Hello world")
.selection( s -> s.color(new Color(80, 140, 255, 90)).borderRadius(6) )
)
)
.withTextSelection("body", selection);
The selection itself, which says which characters are selected, is not part of this
configuration. It is held by the property which you pass to
UIForAnySwing.withTextSelection(swingtree.UI.Layer, String, sprouts.Var).
**So please note that a component without withTextSelection paints no selection at all.**
SwingTree paints the selection colour, and the border if there is one, behind the selected characters and then paints the characters on top of it, in their own colour.
-
Method Summary
Modifier and TypeMethodDescriptionPaints a border of the given width and colour along the inner edge of every area which SwingTree paints behind the selected characters of a line.borderColor(Color color) Sets the colour of the border which SwingTree paints along the inner edge of the selection, seeborder(double, Color).borderRadius(double radius) Rounds every corner of the area which SwingTree paints behind the selected characters, with the same radius for the convex and the concave corners.borderWidth(double width) Sets the width of the border which SwingTree paints along the inner edge of the selection, seeborder(double, Color).caretColor(Color color) Sets the colour of the caret, which SwingTree paints whenshowCaret(boolean)is enabled and no character is selected.caretRadius(double radius) Rounds the corners of the caret, which SwingTree paints whenshowCaret(boolean)is enabled and no character is selected.caretShape(UI.CaretShape shape) Sets the shape of the caret, which SwingTree paints whenshowCaret(boolean)is enabled and no character is selected.caretWidth(double width) Sets how thick the caret is, which SwingTree paints whenshowCaret(boolean)is enabled and no character is selected.Sets the colour which SwingTree paints behind the selected characters.concaveRadius(double radius) Rounds the concave corners of the shape whichmergeLines(boolean)makes from the selected parts of several lines.convexRadius(double radius) Rounds the convex corners of the area which SwingTree paints behind the selected characters.copyToClipboard(boolean copy) Decides whether your users can copy the selected characters.booleaninthashCode()mergeLines(boolean merge) Sets whether SwingTree paints the selected parts of several lines as one shape.navigateWithKeyboard(boolean navigate) Decides whether your users can move the caret with the keyboard, like in a text editor.selectParagraphOnTripleClick(boolean select) Decides whether a triple click selects the paragraph under the mouse.selectWordOnDoubleClick(boolean select) Decides whether a double click selects the word under the mouse.selectWordOnLongPress(boolean select) Decides whether holding the left mouse button down on a word, without moving the mouse, selects that word, like a long press of a finger on the screen of a phone.showCaret(boolean show) Decides whether SwingTree paints the caret when no character is selected.toString()unfocusedBorderColor(Color color) Sets the colour of the border of the selection from the moment the component loses the keyboard focus until it gets it back, seeborder(double, Color)andunfocusedColor(Color).unfocusedColor(Color color) Sets the colour which SwingTree paints behind the selected characters from the moment the component loses the keyboard focus until it gets it back.useUnfocusedLook(boolean use) Decides whether the selection looks different from the moment the component loses the keyboard focus until it gets it back.
-
Method Details
-
color
Sets the colour which SwingTree paints behind the selected characters. A partly transparent colour lets the background of the component shine through. Without this method, the colour is a partly transparent light blue.- Parameters:
color- The colour behind the selected characters.- Returns:
- A new selection configuration with the given colour.
-
borderRadius
Rounds every corner of the area which SwingTree paints behind the selected characters, with the same radius for the convex and the concave corners. It is the same asconvexRadius(radius).concaveRadius(radius), seeconvexRadius(double)andconcaveRadius(double). With a radius of 0, which is the default, the corners are square.- Parameters:
radius- The radius of the quarter circle of every corner, in pixels before the UI scale is applied.- Returns:
- A new selection configuration with the given corner radius.
-
convexRadius
Rounds the convex corners of the area which SwingTree paints behind the selected characters. A convex corner points out of the area, like each of the four corners of a rectangle. WithoutmergeLines(boolean), the area behind the selected characters of every line is a rectangle, so all of its corners are convex. With a border, the outer edge of the border has this radius at a convex corner, and the inner edge of the border has this radius minus the width of the border, or a square corner when the border is wider than the radius. With a radius of 0, which is the default, the convex corners are square.- Parameters:
radius- The radius of the quarter circle of every convex corner, in pixels before the UI scale is applied.- Returns:
- A new selection configuration with the given radius for convex corners.
-
concaveRadius
Rounds the concave corners of the shape whichmergeLines(boolean)makes from the selected parts of several lines. A concave corner points into the shape, like the corner where the end of a short line meets the longer line below it. With a radius above 0, SwingTree fills that corner with a curve, so the outline bends from the edge of the short line into the edge of the longer line. With a border, the outer edge of the border has this radius at a concave corner, and the inner edge of the border has this radius plus the width of the border, so the border is equally wide all along the corner. WithoutmergeLines(true), every area is a rectangle without concave corners, and this radius changes nothing. With a radius of 0, which is the default, the concave corners are square.- Parameters:
radius- The radius of the quarter circle of every concave corner, in pixels before the UI scale is applied.- Returns:
- A new selection configuration with the given radius for concave corners.
-
mergeLines
Sets whether SwingTree paints the selected parts of several lines as one shape. Without this, which is the default, SwingTree paints a separate rectangle behind the selected characters of every line, each with its own rounded corners and its own border. WithmergeLines(true), the rectangles of two neighbouring lines are joined into one shape where they overlap horizontally, and the border runs only along the outline of that shape, never between two lines. Rectangles which do not touch, for example the rectangles on the two sides of an obstacle on the same line, are not joined to each other, and the shape never covers an obstacle. The corners of the shape are rounded withconvexRadius(double)andconcaveRadius(double).- Parameters:
merge-trueto paint the selected parts of neighbouring lines as one shape.- Returns:
- A new selection configuration which joins the lines or not.
-
border
Paints a border of the given width and colour along the inner edge of every area which SwingTree paints behind the selected characters of a line. The border takes its width from the area: the outer edge of the border is where the edge of the area would be without a border, so a selection with a border is exactly as large as a selection without one. The selection colour is painted only inside the border and never under it, so a partly transparent border colour shows the background of the component behind it. How the corners of the border are rounded is described inconvexRadius(double)andconcaveRadius(double). A width of 0, which is the default, paints no border.- Parameters:
width- The width of the border, in pixels before the UI scale is applied.color- The colour of the border.- Returns:
- A new selection configuration with the given border.
-
borderWidth
Sets the width of the border which SwingTree paints along the inner edge of the selection, seeborder(double, Color). The border is only painted when it also has a colour, which you set withborderColor(Color).- Parameters:
width- The width of the border, in pixels before the UI scale is applied.- Returns:
- A new selection configuration with the given border width.
-
borderColor
Sets the colour of the border which SwingTree paints along the inner edge of the selection, seeborder(double, Color). The border is only painted when it also has a width larger than 0, which you set withborderWidth(double).- Parameters:
color- The colour of the border.- Returns:
- A new selection configuration with the given border colour.
-
showCaret
Decides whether SwingTree paints the caret when no character is selected. The caret marks the gap where a click with the shift key held down would continue the selection. It is a thin vertical bar unless you choose another shape withcaretShape(UI.CaretShape). It does not blink. By default, it is not painted, because most styled texts are labels and descriptions which nobody types into. From the moment the component loses the keyboard focus until it gets it back, SwingTree does not paint the caret either, unless you passfalsetouseUnfocusedLook(boolean).- Parameters:
show-trueto paint the caret when no character is selected.- Returns:
- A new selection configuration which paints the caret or not.
-
caretColor
Sets the colour of the caret, which SwingTree paints whenshowCaret(boolean)is enabled and no character is selected. Without this method, the caret has the colour of the font of the text, or black if the font has no single colour.- Parameters:
color- The colour of the caret.- Returns:
- A new selection configuration with the given caret colour.
-
caretShape
Sets the shape of the caret, which SwingTree paints whenshowCaret(boolean)is enabled and no character is selected. AUI.CaretShape.BARis a vertical bar in the gap, aUI.CaretShape.BLOCKcovers the character behind the gap, and aUI.CaretShape.UNDERLINEis a line under that character. Behind the last character of a line, where no character follows the gap, a block and an underline are half as wide as the line is tall. SwingTree paints the caret behind the characters, so give a block a colour in which the character on top of it stays readable, seecaretColor(Color). Without this method, the caret is aUI.CaretShape.BAR.- Parameters:
shape- The shape of the caret.- Returns:
- A new selection configuration with the given caret shape.
-
caretWidth
Sets how thick the caret is, which SwingTree paints whenshowCaret(boolean)is enabled and no character is selected. It is the width of aUI.CaretShape.BAR, which grows to the right of the gap, and the height of anUI.CaretShape.UNDERLINE, which grows upwards from the bottom of the line. AUI.CaretShape.BLOCKis as wide as the character it covers, so this width does not change it. However thin you make it, SwingTree paints the caret at least one pixel thick on the screen. Without this method, the caret is 1 pixel thick.- Parameters:
width- The thickness of the caret, in pixels before the UI scale is applied.- Returns:
- A new selection configuration with the given caret width.
-
caretRadius
Rounds the corners of the caret, which SwingTree paints whenshowCaret(boolean)is enabled and no character is selected. Every corner of the caret becomes a quarter circle with this radius, or with half the length of the shorter side of the caret when this radius is larger. So any radius of at least half of thecaretWidth(double)turns aUI.CaretShape.BARinto a thin pill with round ends, and anUI.CaretShape.UNDERLINEinto a flat pill. The rounding only cuts the corners off, so the caret never reaches outside ofTextSelection.caretBounds(). With a radius of 0, which is the default, the corners are square:.selection( s -> s.showCaret(true).caretWidth(3).caretRadius(1.5) )- Parameters:
radius- The radius of the quarter circle of every corner of the caret, in pixels before the UI scale is applied.- Returns:
- A new selection configuration with the given corner radius for the caret.
-
unfocusedColor
Sets the colour which SwingTree paints behind the selected characters from the moment the component loses the keyboard focus until it gets it back. Many programs, like the text fields of web browsers and of macOS, grey a selection when the keyboard focus goes elsewhere, so that your users can tell at a glance where the keys they type go to:
A component which never had the keyboard focus looks focused. Without this method, SwingTree takes the colour of.selection( s -> s .color(new Color(80, 140, 255, 90)) .unfocusedColor(new Color(160, 160, 160, 90)) )color(Color)and removes most of its saturation while it keeps its transparency and its brightness, so the selection turns grey but stays as easy to see as before. WithuseUnfocusedLook(false), SwingTree never paints this colour.- Parameters:
color- The colour behind the selected characters after the component lost the keyboard focus.- Returns:
- A new selection configuration with the given colour for an unfocused component.
-
unfocusedBorderColor
Sets the colour of the border of the selection from the moment the component loses the keyboard focus until it gets it back, seeborder(double, Color)andunfocusedColor(Color). This colour only replaces the colour of a border which SwingTree paints while the component has the focus, so a selection without a border colour or without a border width gets no border from it. Without this method, SwingTree takes the colour ofborderColor(Color)and removes most of its saturation while it keeps its transparency and its brightness.- Parameters:
color- The colour of the border after the component lost the keyboard focus.- Returns:
- A new selection configuration with the given border colour for an unfocused component.
-
useUnfocusedLook
Decides whether the selection looks different from the moment the component loses the keyboard focus until it gets it back. A component which never had the keyboard focus always looks focused. By default, which istrue, SwingTree paints the selection in the colours ofunfocusedColor(Color)andunfocusedBorderColor(Color)and paints no caret from the moment the component loses the keyboard focus until it gets it back. Withfalse, the keyboard focus changes nothing that your users can see: the selection keeps the colours ofcolor(Color)andborderColor(Color), and the caret ofshowCaret(boolean)stays where it is. This suits an application whose look should not change with the keyboard focus, and a test which compares pictures of the component taken while the keyboard focus is somewhere else.- Parameters:
use-trueto grey the selection and hide the caret after the component lost the keyboard focus.- Returns:
- A new selection configuration which changes its look with the keyboard focus or not.
-
selectWordOnDoubleClick
Decides whether a double click selects the word under the mouse. A word is whatBreakIterator.getWordInstance()finds around the character under the mouse, which follows the word rules of many languages, so "don't" is one word. The arrow keys with Ctrl held down, seenavigateWithKeyboard(boolean), jump over words by the same rules. By default, which istrue, a double click selects the word. Withfalse, the second click of a double click does what a single click does: it puts the caret into the gap nearest to the mouse.- Parameters:
select-trueto select the word under the mouse on a double click.- Returns:
- A new selection configuration which selects a word on a double click or not.
-
selectParagraphOnTripleClick
Decides whether a triple click selects the paragraph under the mouse. A paragraph is everything between two line breaks of the text, the'\n'characters, however many painted lines it wraps over. The line breaks themselves are not selected, so a paragraph which your users copy does not end with an empty line. By default, which istrue, a triple click selects the paragraph. Withfalse, the third click of a triple click does what a single click does: it puts the caret into the gap nearest to the mouse..selection( s -> s.selectParagraphOnTripleClick(false) )- Parameters:
select-trueto select the paragraph under the mouse on a triple click.- Returns:
- A new selection configuration which selects a paragraph on a triple click or not.
-
selectWordOnLongPress
Decides whether holding the left mouse button down on a word, without moving the mouse, selects that word, like a long press of a finger on the screen of a phone. After about half a second the word under the mouse is selected. A movement of up to 5 pixels before then is allowed, because a finger never holds perfectly still. Like every size of a style, these 5 pixels are multiplied by the UI scale, so they are 10 pixels on the screen at a UI scale of 2. Moving further, releasing the button earlier, or the component losing the keyboard focus cancels it, and then a press only puts the caret, as it does without a long press. By default, which isfalse, holding the button down does nothing more than a click, because on a desktop your users select a word with a double click..selection( s -> s.selectWordOnLongPress(true) )- Parameters:
select-trueto select the word under the mouse when the button is held down without moving.- Returns:
- A new selection configuration which selects a word on a long press or not.
-
copyToClipboard
Decides whether your users can copy the selected characters. By default, which istrue, Ctrl+C, or Command+C on macOS, puts the selected characters onto the clipboard of the computer, and so does theTransferHandlerof the component when your code callsTransferHandler.exportToClipboard(javax.swing.JComponent, java.awt.datatransfer.Clipboard, int). Withfalse, nothing reaches any clipboard, and whatever is on the clipboard stays there, for example for a text which your users may read and select but should type themselves. Your users can still select the characters, also everything at once with Ctrl+A or Command+A.- Parameters:
copy-trueto let your users copy the selected characters.- Returns:
- A new selection configuration which lets your users copy or not.
-
equals
-
hashCode
public int hashCode() -
toString
-