Skip to content

Commit

Permalink
Improved StlysheetLength implementation and added two new unit types …
Browse files Browse the repository at this point in the history
…`dprd` and `dpru`.

Improved Tabs visibility when they are closable.
Fix minor bug in ProjectDirectoryTree.
Fixed a regression on UINodeDrawable.
  • Loading branch information
SpartanJ committed Nov 18, 2023
1 parent 6d5e453 commit c2fbda6
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 73 deletions.
6 changes: 1 addition & 5 deletions bin/assets/ui/breeze.css
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ Tab {
height: 24dp;
transition: background-color 0.15s;
foreground-image: linear-gradient( to top, var(--tab-line), var(--tab-back));
foreground-size: 1dp 80%;
foreground-size: 1dprd 80%;
foreground-position: right bottom;
}

Expand Down Expand Up @@ -586,9 +586,6 @@ Tab::close {
width: 10dp;
height: 10dp;
border-radius: 5dp;
/*background-color: var(--tab-close);
foreground-image: poly(line, var(--icon-line-hover), "0dp 0dp, 5dp 5dp"), poly(line, var(--icon-line-hover), "5dp 0dp, 0dp 5dp");
foreground-position: 2.5dp 2.5dp, 2.5dp 2.5dp;*/
foreground-image: url("data:image/svg,<svg width='16' height='16' viewBox='0 0 16 16'><path fill='#ffffff' fill-rule='evenodd' d='M 2.3432061,13.657206 A 8.0002061,8.0002061 0 1 1 13.657206,2.3432061 8.0002061,8.0002061 0 0 1 2.3432061,13.657206 Z m 3.687,-8.6869999 a 0.75,0.75 0 0 0 -1.06,1.06 l 1.97,1.97 -1.97,1.97 a 0.75,0.75 0 1 0 1.06,1.0599999 l 1.97,-1.9699999 1.97,1.9699999 A 0.75,0.75 0 1 0 11.030206,9.9702061 l -1.9699999,-1.97 1.9699999,-1.97 a 0.75,0.75 0 1 0 -1.0599999,-1.06 l -1.97,1.97 z' /></svg>");
foreground-tint: var(--tab-close);
foreground-size: 10dp 10dp;
Expand All @@ -597,7 +594,6 @@ Tab::close {
}

Tab::close:hover {
/*background-color: var(--tab-close-hover);*/
foreground-tint: var(--tab-close-hover);
}

Expand Down
5 changes: 3 additions & 2 deletions docs/articles/cssspecification.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ the CSS file.
* eepp CSS supports alternative path methods besides paths (resource locator to previously loaded
resources by the process).

* eepp CSS supports [Device-independent
pixel](https://en.wikipedia.org/wiki/Device-independent_pixel) unit `dp`.
* eepp CSS supports [Device-independent pixel](https://en.wikipedia.org/wiki/Device-independent_pixel) unit `dp`.

* CSS files should be always UTF-8 encoded.

Expand Down Expand Up @@ -2461,6 +2460,8 @@ Read [length](https://developer.mozilla.org/en-US/docs/Web/CSS/length) documenta

* Supported lenghts: `em`, `rem`, `pt`, `pc`, `in`, `cm`, `mm`, `vw`, `vh`, `vmin`, `vmax`.

* Also adds: `dp` as [Device-independent pixel](https://en.wikipedia.org/wiki/Device-independent_pixel). Plus `dprd` (dp rounded down) and `dpru` (dp rounded up).

---

### length-percentage (data-type)
Expand Down
4 changes: 3 additions & 1 deletion include/eepp/ui/css/stylesheetlength.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class EE_API StyleSheetLength {
Vmin,
Vmax,
Rem,
Dprd,
Dpru,
};

static Unit unitFromString( std::string unitStr );
Expand Down Expand Up @@ -64,7 +66,7 @@ class EE_API StyleSheetLength {

StyleSheetLength& operator=( const Float& val );

static StyleSheetLength fromString( std::string str, const Float& defaultValue = 0 );
static StyleSheetLength fromString( const std::string& str, const Float& defaultValue = 0 );

std::string toString() const;

Expand Down
7 changes: 3 additions & 4 deletions include/eepp/ui/css/stylesheetselector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ class EE_API StyleSheetSelector {

bool select( UIWidget* element, const bool& applyPseudo = true ) const;

const bool& isCacheable() const;
bool isCacheable() const;

bool hasPseudoClasses() const;

std::vector<UIWidget*> getRelatedElements( UIWidget* element,
const bool& applyPseudo = true ) const;
std::vector<UIWidget*> getRelatedElements( UIWidget* element, bool applyPseudo = true ) const;

const bool& isStructurallyVolatile() const;
bool isStructurallyVolatile() const;

const StyleSheetSelectorRule& getRule( const Uint32& index );

Expand Down
3 changes: 2 additions & 1 deletion projects/linux/ee.creator.user
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 11.0.3, 2023-11-16T01:44:57. -->
<!-- Written by QtCreator 11.0.3, 2023-11-18T11:06:20. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
Expand Down Expand Up @@ -1215,6 +1215,7 @@
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">ecode-debug</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.CustomExecutableRunConfiguration</value>
<value type="QString" key="ProjectExplorer.RunConfiguration.BuildKey"></value>
<value type="QString" key="RunConfiguration.Arguments">--css=/root/.config/ecode/style.css</value>
<value type="int" key="RunConfiguration.UseCppDebugger">0</value>
<value type="bool" key="RunConfiguration.UseCppDebuggerAuto">false</value>
<value type="int" key="RunConfiguration.UseQmlDebugger">1</value>
Expand Down
171 changes: 121 additions & 50 deletions src/eepp/ui/css/stylesheetlength.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,115 @@
#include <eepp/ui/css/stylesheetlength.hpp>

using namespace EE::Graphics;
using namespace std::literals;

namespace EE { namespace UI { namespace CSS {

enum UnitHashes : String::HashType {
Percentage = String::hash( "%" ),
In = String::hash( "in" ),
Cm = String::hash( "cm" ),
Mm = String::hash( "mm" ),
Em = String::hash( "em" ),
Ex = String::hash( "ex" ),
Pt = String::hash( "pt" ),
Pc = String::hash( "pc" ),
Px = String::hash( "px" ),
Dpi = String::hash( "dpi" ),
Dp = String::hash( "dp" ),
Dpcm = String::hash( "dpcm" ),
Vw = String::hash( "vw" ),
Vh = String::hash( "vh" ),
Vmin = String::hash( "vmin" ),
Vmax = String::hash( "vmax" ),
Rem = String::hash( "rem" ),
Dprd = String::hash( "dprd" ),
Dpru = String::hash( "dpru" ),
};

enum PercentagePositions : String::HashType {
Center = String::hash( "center" ),
Left = String::hash( "left" ),
Right = String::hash( "right" ),
Top = String::hash( "top" ),
Bottom = String::hash( "bottom" ),
None = 0,
};

static std::string positionToPercentage( const PercentagePositions& pos ) {
switch ( pos ) {
case Center:
return "50%";
case Left:
case Top:
return "0%";
case Right:
case Bottom:
return "100%";
default:
case None:
return "";
}
}

static PercentagePositions isPercentagePosition( const String::HashType& strHash ) {
switch ( strHash ) {
case PercentagePositions::Center:
return PercentagePositions::Center;
case PercentagePositions::Left:
return PercentagePositions::Left;
case PercentagePositions::Right:
return PercentagePositions::Right;
case PercentagePositions::Top:
return PercentagePositions::Top;
case PercentagePositions::Bottom:
return PercentagePositions::Bottom;
}
return PercentagePositions::None;
}

StyleSheetLength::Unit StyleSheetLength::unitFromString( std::string unitStr ) {
String::toLowerInPlace( unitStr );
if ( "%" == unitStr )
return Unit::Percentage;
else if ( "dp" == unitStr )
return Unit::Dp;
else if ( "px" == unitStr )
return Unit::Px;
else if ( "in" == unitStr )
return Unit::In;
else if ( "cm" == unitStr )
return Unit::Cm;
else if ( "mm" == unitStr )
return Unit::Mm;
else if ( "em" == unitStr )
return Unit::Em;
else if ( "ex" == unitStr )
return Unit::Ex;
else if ( "pt" == unitStr )
return Unit::Pt;
else if ( "pc" == unitStr )
return Unit::Pc;
else if ( "dpi" == unitStr )
return Unit::Dpi;
else if ( "dpcm" == unitStr )
return Unit::Dpcm;
else if ( "vw" == unitStr )
return Unit::Vw;
else if ( "vh" == unitStr )
return Unit::Vh;
else if ( "vmin" == unitStr )
return Unit::Vmin;
else if ( "vmax" == unitStr )
return Unit::Vmax;
else if ( "rem" == unitStr )
return Unit::Rem;
switch ( String::hash( unitStr ) ) {
case UnitHashes::Percentage:
return Unit::Percentage;
case UnitHashes::Dp:
return Unit::Dp;
case UnitHashes::Px:
return Unit::Px;
case UnitHashes::In:
return Unit::In;
case UnitHashes::Cm:
return Unit::Cm;
case UnitHashes::Mm:
return Unit::Mm;
case UnitHashes::Em:
return Unit::Em;
case UnitHashes::Ex:
return Unit::Ex;
case UnitHashes::Pt:
return Unit::Pt;
case UnitHashes::Pc:
return Unit::Pc;
case UnitHashes::Dpi:
return Unit::Dpi;
case UnitHashes::Dpcm:
return Unit::Dpcm;
case UnitHashes::Vw:
return Unit::Vw;
case UnitHashes::Vh:
return Unit::Vh;
case UnitHashes::Vmin:
return Unit::Vmin;
case UnitHashes::Vmax:
return Unit::Vmax;
case UnitHashes::Rem:
return Unit::Rem;
case UnitHashes::Dprd:
return Unit::Dprd;
case UnitHashes::Dpru:
return Unit::Dpru;
}
return Unit::Px;
}

Expand Down Expand Up @@ -82,6 +152,10 @@ std::string StyleSheetLength::unitToString( const StyleSheetLength::Unit& unit )
return "vmax";
case Unit::Rem:
return "rem";
case Unit::Dprd:
return "dprd";
case Unit::Dpru:
return "dpru";
}
return "px";
}
Expand Down Expand Up @@ -123,6 +197,12 @@ Float StyleSheetLength::asPixels( const Float& parentSize, const Sizef& viewSize
case Unit::Dp:
ret = PixelDensity::dpToPx( mValue );
break;
case Unit::Dprd:
ret = roundDown( PixelDensity::dpToPx( mValue ) );
break;
case Unit::Dpru:
ret = roundUp( PixelDensity::dpToPx( mValue ) );
break;
case Unit::Em:
ret = Math::round( mValue * elFontSize );
break;
Expand Down Expand Up @@ -190,22 +270,15 @@ StyleSheetLength& StyleSheetLength::operator=( const StyleSheetLength& val ) {
return *this;
}

static std::string positionToPercentage( const std::string& pos ) {
if ( pos == "center" )
return "50%";
if ( pos == "left" || pos == "top" )
return "0%";
if ( pos == "right" || pos == "bottom" )
return "100%";
return pos;
}
StyleSheetLength StyleSheetLength::fromString( const std::string& str, const Float& defaultValue ) {
PercentagePositions isPercentage = isPercentagePosition( String::hash( str ) );
if ( PercentagePositions::None != isPercentage )
return fromString( positionToPercentage( isPercentage ), defaultValue );

StyleSheetLength StyleSheetLength::fromString( std::string str, const Float& defaultValue ) {
StyleSheetLength length;
length.setValue( defaultValue, Unit::Px );
std::string num;
std::string unit;
str = positionToPercentage( str );

for ( std::size_t i = 0; i < str.size(); i++ ) {
if ( String::isNumber( str[i], true ) || ( '-' == str[i] && i == 0 ) ||
Expand All @@ -219,11 +292,8 @@ StyleSheetLength StyleSheetLength::fromString( std::string str, const Float& def

if ( !num.empty() ) {
Float val = 0;
bool res = String::fromString<Float>( val, num );

if ( res ) {
if ( String::fromString<Float>( val, num ) )
length.setValue( val, unitFromString( unit ) );
}
}

return length;
Expand All @@ -233,7 +303,8 @@ std::string StyleSheetLength::toString() const {
std::string res;
if ( (Int64)mValue == mValue )
res = String::format( "%lld%s", (Int64)mValue, unitToString( mUnit ).c_str() );
res = String::format( "%.2f%s", mValue, unitToString( mUnit ).c_str() );
else
res = String::format( "%.2f%s", mValue, unitToString( mUnit ).c_str() );
String::replace( res, ",", "." );
return res;
}
Expand Down
6 changes: 3 additions & 3 deletions src/eepp/ui/css/stylesheetselector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ void StyleSheetSelector::parseSelector( std::string selector ) {
}
}

const bool& StyleSheetSelector::isCacheable() const {
bool StyleSheetSelector::isCacheable() const {
return mCacheable;
}

Expand Down Expand Up @@ -210,7 +210,7 @@ bool StyleSheetSelector::select( UIWidget* element, const bool& applyPseudo ) co
}

std::vector<UIWidget*> StyleSheetSelector::getRelatedElements( UIWidget* element,
const bool& applyPseudo ) const {
bool applyPseudo ) const {
static std::vector<UIWidget*> EMPTY_ELEMENTS;
std::vector<UIWidget*> elements;
if ( mSelectorRules.empty() )
Expand Down Expand Up @@ -331,7 +331,7 @@ std::vector<UIWidget*> StyleSheetSelector::getRelatedElements( UIWidget* element
return elements;
}

const bool& StyleSheetSelector::isStructurallyVolatile() const {
bool StyleSheetSelector::isStructurallyVolatile() const {
return mStructurallyVolatile;
}

Expand Down
2 changes: 1 addition & 1 deletion src/eepp/ui/uinodedrawable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ void UINodeDrawable::LayerDrawable::update() {
setDrawable( mDrawableRef );
}

mDrawableSize = calcDrawableSize( mSizeEq ).roundDown();
mDrawableSize = calcDrawableSize( mSizeEq );
mOffset = calcPosition( mPositionX + " " + mPositionY );

mNeedsUpdate = false;
Expand Down
5 changes: 2 additions & 3 deletions src/eepp/ui/uitabwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ UITab* UITabWidget::createTab( const String& text, UINode* nodeOwned, Drawable*
tab->setVisible( true );
tab->setEnabled( true );
tab->setOwnedWidget( nodeOwned );
tab->reloadStyle( true, true, true );
if ( tab->getCloseButton() ) {
tab->getCloseButton()
->setVisible( mStyleConfig.TabsClosable && mStyleConfig.TabCloseButtonVisible )
Expand Down Expand Up @@ -752,9 +753,7 @@ UITab* UITabWidget::setTabSelected( UITab* tab ) {
if ( tab->getOwnedWidget() )
tab->getOwnedWidget()->setFocus();
return tab;
}

if ( NULL != mTabSelected ) {
} else if ( NULL != mTabSelected ) {
mTabSelected->unselect();

if ( NULL != mTabSelected->getOwnedWidget() ) {
Expand Down
7 changes: 7 additions & 0 deletions src/tools/ecode/applayout.xml.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,13 @@ Anchor.error:hover {
#code_container > TabWidget > TabWidget::TabBar > Tab > Tab::Text {
text-overflow: ellipsis;
}
#code_container > TabWidget > TabWidget::TabBar > Tab > Tab::close {
opacity: 0;
}
#code_container > TabWidget > TabWidget::TabBar > Tab:selected > Tab::close,
#code_container > TabWidget > TabWidget::TabBar > Tab:hover > Tab::close {
opacity: 1;
}
</style>
<MainLayout id="main_layout" lw="mp" lh="mp">
<Splitter id="project_splitter" lw="mp" lh="mp">
Expand Down
Loading

0 comments on commit c2fbda6

Please sign in to comment.